2012-02-22 76 views
5

Mi intención es crear una macro muy básica para encontrar una serie de palabras y resaltarlas. Lamentablemente, no sé cómo hacer varias palabras en un solo paso. Por ejemplo, el siguiente código funciona:Microsoft Word Macro para resaltar varias palabras

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 

Sin embargo, si añado en otra línea .Text =, entonces el MJ: se ignora. ¿Algunas ideas?

Respuesta

3

Si solo busca unas pocas palabras, simplemente haciendo múltiples hallazgos y reemplazos dentro de la misma macro logrará lo que quiere. Por ejemplo, la siguiente destacará en amarillo todas las apariciones de "destino1" y "destino2"

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 

Alternativamente el siguiente código le permitirá añadir todos los términos para destacar en una línea que puede ser más fácil de trabajar.

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

Gracias! Ese bucle era exactamente lo que estaba buscando. Me gustaría poder decir que entiendo cómo funciona, pero si funciona, ¡entonces estoy feliz! –

Cuestiones relacionadas