Share via

Reset style selection in Find/Replace window

Anonymous
2020-08-12T20:41:03+00:00

In a Word macro, I have the following statement:

Selection.Find.Style = ActiveDocument.Styles("NeedsAttention")

After I execute the Find/Replace, I want to reset the Selection.Find and Selection.Find.Replacement properties. How do I remove this style setting and set it to nothing? Selection.Find.ClearFormatting doesn't do it.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2020-08-12T23:48:39+00:00

I wouldn't advise using ActiveDocument.Content.Find directly unless you're doing a ReplaceAll, because the Content object always points to the entire range of the document.

If you instead declare a Range object and initialize it to ActiveDocument.Content or ActiveDocument.Range, then that Range object gets restricted by the Find.Execute statement each time it finds the specified text, pointing only to that specific piece of the text. This is a typical (although useless) example:

Sub x()

    Dim rg As Range

    Set rg = ActiveDocument.Content

    With rg.Find

        .Text = "Word"

        .Forward = True

        .Wrap = wdFindStop

        While .Execute

            rg.Font.ColorIndex = wdRed

            rg.Collapse wdCollapseEnd

        Wend

    End With

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Stefan Blom 342.6K Reputation points MVP Volunteer Moderator
2020-08-12T23:02:13+00:00

The two statements Charles suggested should certainly clear styles from the Find & Replace dialog box. It works here (Office 2019, version 2007).

If possible, use Find with a Range object instead. Then there will be no need to reset formatting. A basic example:

With ActiveDocument.Content.Find

'code goes here...

End With

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2020-08-12T21:14:57+00:00

    Selection.Find.ClearFormatting clears the Find box.

    Selection.Find.Replacement.ClearFormatting clears the Replace box formatting.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2020-08-12T21:20:58+00:00

    Yes, but it doesn't remove the style that was set with:

    Selection.Find.Style = ActiveDocument.Styles("NeedsAttention")

    How do I remove the style setting?

    When I first start Word and open the Find/Replace window, there are no styles selected for the Find text and no styles selected for the Replace text. That's what I'm trying to get back to in my macro after a style has been set for the Find text.

    In the Word user interface, I can select "(no style)" in Find > Format > Style. How do I mimic this in a macro?

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Stefan Blom 342.6K Reputation points MVP Volunteer Moderator
    2020-08-13T09:11:13+00:00

    Good point. Also, I often use the Duplicate method when working on ranges, just in case.

    Was this answer helpful?

    0 comments No comments