none
Problem finding highlighted text RRS feed

  • Question

  • We have a document with a paragraph of text followed immediately by a table. In the paragraph is some text which is highlighted. We want to find this highlighted text using the "find" dialogue. The code is as follows

           Selection.Find.ClearFormatting
           Selection.Find.Highlight = True
           Selection.Find.Forward = True
           With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
           End With
           Selection.Find.Execute

    We place the cursor at the start of the paragraph and when the code is executed the selection found is the next bit of highlighted text, except it does not include the last character of the highlighted text.

    If we add a new paragraph between the original paragraph and the table, the complete highlighted text is found correctly.

    Any suggestions as to why this is happening?

    Thursday, November 29, 2012 1:23 PM

Answers

  • I'd say it's a bug. If it's any comfort, the same behaviour occurs in both Word 2003 & 2010. Try this workaround:

    With Selection
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = True
        .Highlight = True
        .Text = ""
        .Replacement.Text = ""
        .Execute
        .Wrap = wdFindStop
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
      End With
      While .Characters.Last.Next.HighlightColorIndex <> wdNoHighlight
        .MoveEnd wdCharacter, 1
      Wend
    End With

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 30, 2012 4:43 AM