none
Change formatting of one word in a search string RRS feed

  • Question

  • I'm new to VBA and haven't been able to accomplish this. I've searched and still have no answer. 

    I need a macro that will search a document for a string and then change the formatting on a single word in that string. I have tried to record a macro to do that but if the recorder is running, it won't allow me to select a single word from the search string. So, that didn't work either. Specifically, I need to find every occurrence of 

    Status: Open

    and change the formatting of 'Open' to red and bold.

    Thanks for your help!

    Wednesday, December 13, 2017 2:12 AM

All replies

  • Try this macro:

    Sub FormatText()
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Text = "Status: Open"
            .Replacement.ClearFormatting
            Do While .Execute
                Selection.MoveStart Unit:=wdWord
                With Selection.Font
                    .Bold = True
                    .ColorIndex = wdRed
                End With
                Selection.Collapse Direction:=wdCollapseEnd
            Loop
        End With
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, December 13, 2017 5:10 AM
  • Somewhat faster:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Status: Open"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        With .Words.Last.Font
          .Bold = True
          .ColorIndex = wdRed
        End With
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Wednesday, December 13, 2017 7:38 AM