2012-02-22 48 views
5

Mục đích của tôi là tạo macro rất cơ bản để tìm chuỗi từ và đánh dấu chúng. Thật không may, tôi không biết làm thế nào để làm nhiều từ trong một bước. Ví dụ, đoạn mã sau hoạt động:Macro Microsoft Word để làm nổi bật nhiều từ

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 

Tuy nhiên, nếu tôi thêm vào một dòng .Text = thì MJ: được bỏ qua. Ý tưởng nào?

Trả lời

3

Nếu bạn chỉ tìm kiếm một vài từ chỉ cần thực hiện nhiều tìm và thay thế trong cùng một macro sẽ thực hiện những gì bạn muốn. Ví dụ, sau đây sẽ làm nổi bật màu vàng tất cả các lần xuất hiện của "target1" và "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 

Ngoài ra đoạn mã sau sẽ cho phép bạn thêm tất cả các điều khoản để làm nổi bật trong một dòng có thể được dễ dàng hơn để làm việc với.

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

Cảm ơn! Vòng lặp đó chính là thứ tôi đang tìm kiếm. Tôi ước tôi có thể nói tôi hiểu nó hoạt động như thế nào, nhưng nếu nó hoạt động thì tôi hạnh phúc! –

Các vấn đề liên quan