0

我有一个简单的宏,以便在单词的前后插入一个符号。

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

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

4

1 回答 1

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
于 2013-08-27T17:36:47.913 回答