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

时间:2013-08-27 15:16:08

标签: vba loops ms-word word-vba

我有一个简单的宏,以便在单词的正面和背面插入符号。

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

有没有办法让宏在文本选择中循环遍历每个单词?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

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