2012-02-22 26 views
5

Il mio intento è creare una macro molto semplice per trovare una serie di parole e evidenziarle. Sfortunatamente, non so come fare più parole in un solo passaggio. Ad esempio, il seguente codice funziona:Microsoft Word Macro per evidenziare più parole

Sub Macro1() 
' 
' Macro1 Macro 
' 
' 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    Selection.Find.Replacement.Highlight = True 
    With Selection.Find 
     .Text = "MJ:" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = True 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
End Sub 

Tuttavia, se si aggiunge a un'altra linea .Text =, allora la MJ: viene ignorato. Qualche idea?

risposta

3

Se si cercano solo poche parole, è sufficiente eseguire più ricerche e sostituzioni all'interno della stessa macro per ottenere ciò che si desidera. Ad esempio, il seguente evidenzierà in giallo tutte le occorrenze di "target1" e "TARGET2"

Sub HighlightTargets() 

' --------CODE TO HIGHLIGHT TARGET 1------------------- 
    Options.DefaultHighlightColorIndex = wdYellow 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    Selection.Find.Replacement.Highlight = True 
    With Selection.Find 
     .Text = "target1" 
     .Replacement.Text = "target1" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

' --------CODE TO HIGHLIGHT TARGET 1------------------- 
    Options.DefaultHighlightColorIndex = wdYellow 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    Selection.Find.Replacement.Highlight = True 
    With Selection.Find 
     .Text = "target2" 
     .Replacement.Text = "target2" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
End Sub 

In alternativa il seguente codice vi permetterà di aggiungere tutti i termini per evidenziare in una linea che può essere più facile lavorare con.

Sub HighlightTargets2() 

Dim range As range 
Dim i As Long 
Dim TargetList 

TargetList = Array("target1", "target2", "target3") ' put list of terms to find here 

For i = 0 To UBound(TargetList) 

Set range = ActiveDocument.range 

With range.Find 
.Text = TargetList(i) 
.Format = True 
.MatchCase = True 
.MatchWholeWord = False 
.MatchWildcards = False 
.MatchSoundsLike = False 
.MatchAllWordForms = False 

Do While .Execute(Forward:=True) = True 
range.HighlightColorIndex = wdYellow 

Loop 

End With 
Next 

End Sub 
+0

Grazie! Quel ciclo era esattamente quello che stavo cercando. Vorrei poter dire di capire come funziona, ma se funziona allora sono felice! –

Problemi correlati