简体   繁体   English

如何在选择中逐字循环应用MS Word宏?

[英]How to loop a MS Word macro applying word by word in a selection?

I have a simple macro in order to insert a symbol into the front and back of a word. 我有一个简单的宏,以便将符号插入单词的前面和后面。

Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="a"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Color = -603914241
Selection.Font.Size = 1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="a"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Color = -603914241
Selection.Font.Size = 1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1

Is there any way to make the macros looping through each word in the selection of text? 有什么方法可以使宏在选择的文本中循环遍历每个单词?

You can do something like this: 您可以执行以下操作:

Dim myRangeIndex As Range
Dim myRangeStart As Range

Set myRangeStart = Selection.Range

For Each myRangeIndex In myRangeStart.Words()

   myRangeIndex.Select

   Selection.MoveLeft Unit:=wdWord, Count:=1
   Selection.TypeText Text:="a"
   Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
   Selection.Font.Color = -603914241
   Selection.Font.Size = 1
   Selection.MoveRight Unit:=wdWord, Count:=1
   Selection.MoveLeft Unit:=wdWord, Count:=1
   Selection.MoveRight Unit:=wdWord, Count:=1
   Selection.MoveLeft Unit:=wdCharacter, Count:=1
   Selection.TypeText Text:="a"
   Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
   Selection.Font.Color = -603914241
   Selection.Font.Size = 1
   Selection.MoveRight Unit:=wdCharacter, Count:=1
   Selection.MoveRight Unit:=wdWord, Count:=1

Next myRangeIndex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM