2013-05-14 49 views
1

我在Word 2007中記錄了一個查找單詞的宏,將光標向上移動兩行,插入三個'***',然後突出顯示該行。它適用於找到的單詞的第一個實例。我正努力讓它在整個文檔中重複出現我想要查找的單詞的所有實例。查找文本和格式

這是我錄製的宏的輸出。我需要對每個「B」實例重複這些操作。

Sub HighlightNewItems() 
' 
' HighlightNewItems Macro 
' 
' 
    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Text = "B," 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = True 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.MoveUp Unit:=wdLine, Count:=2 
    Selection.MoveLeft Unit:=wdWord, Count:=1 
    Selection.TypeText Text:="***" 
    Selection.TypeParagraph 
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend 
    Options.DefaultHighlightColorIndex = wdRed 
    Selection.Range.HighlightColorIndex = wdRed 
    Selection.MoveRight Unit:=wdCharacter, Count:=1 
End Sub 

回答

1

嘗試把在您With.Selection.Find

Do While .Execute 
    '(logic that you want to apply after finding string) 
Loop 

在你的情況下,下面的結構,你的代碼看起來像

Sub HighlightNewItems() 
' 
' HighlightNewItems Macro 
' 
' 
    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Text = "B," 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = True 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 

     Do While .Execute 
     Selection.MoveUp Unit:=wdLine, Count:=2 
     Selection.MoveLeft Unit:=wdWord, Count:=1 
     Selection.TypeText Text:="***" 
     Selection.TypeParagraph 
     Selection.EndKey Unit:=wdLine, Extend:=wdExtend 
     Options.DefaultHighlightColorIndex = wdRed 
     Selection.Range.HighlightColorIndex = wdRed 
     Selection.MoveRight Unit:=wdCharacter, Count:=1 
     Loop 

    End With 

End Sub