0
votes

What I need to do is add a Rich Text Content Control to all "xx"'s in a large word document.

I saw a similar thread on highlighting multiple instances. I based the solution that I've tried so far on this:

    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

Now just testing in word to see what command it uses to add the content controls individually I found this command:

Selection.Range.ContentControls.Add (wdContentControlRichText)

What I came up with was:

    Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Range.ContentControls.Add (wdContentControlRichText)
With Selection.Find
    .Text = "xx"
    .Replacement.Text = "xx"
    .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

Swapping out Selection.Range with Selection.Find simply resulted in a "method or data field not found" or something like that. I think Range refers to the currently highlighted range of characters, whereas find refers to whatever the with block... well, finds.

Whatever find is doesn't appear to have the ability to throw some content controls onto it.

1

1 Answers

0
votes

You were quite close:

Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
    .Text = "xx"
    '.Replacement.Text = "xx"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Do While Selection.Find.Execute

    'Selection.Text = "" 'uncomment if you want to remove xx
    Selection.Range.ContentControls.Add (wdContentControlRichText)

Loop
End Sub