Share via

Macro for advanced find and replace

Anonymous
2011-08-04T20:24:59+00:00

I need to find bold words and apply a character format to them. I also need to do the same for italic and underlined words. nothing I record seems to work. Here is a sample:

Sub Macro1()

'

' Macro1 Macro

'

'

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

Selection.Find.Replacement.Style = ActiveDocument.Styles("bold")

With Selection.Find

.Text = "(?)"

.Replacement.Text = "\1"

.Forward = True

.Wrap = wdFindContinue

.Format = True

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = True

.MatchSoundsLike = False

.MatchAllWordForms = False

End With

Selection.Find.Execute Replace:=wdReplaceAll

End Sub

This applies the bold style to all text, not just text that's already bold.

Any help will be appreciated.

I'd also like to find the directory of VBA terms.

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

Anonymous
2011-08-04T20:52:57+00:00

Your macro doesn't tell Word to just find bold characters, it finds ALL characters. To limit the replacement to bold characters you need to set the Find object's .Font.Bold property to True, e.g.:

With Selection.Find

.ClearFormatting

.Font.Bold = True

.Text = "(?)"

With .Replacement

.ClearFormatting

.Style = ActiveDocument.Styles("bold")

.Text = "\1"

End With

.Forward = True

.Wrap = wdFindContinue

.Format = True

.MatchCase = False

.MatchWholeWord = False

.MatchSoundsLike = False

.MatchAllWordForms = False

.MatchWildcards = True

.Execute Replace:=wdReplaceAll

End With

Was this answer helpful?

0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-08-04T22:26:17+00:00

    Two different ways to accomplish the same thing.  The one I provided is simplier and faster in execution and in writing. All you had to do was replace the line that showed you the text with the debug.print rng.text command to rng.style = word.activedocument.styles("bold")

    Selection commands work off the screen, they are slower, cause screen flicker (which bothers some people), and depending on what exactly is being done can be prone to error particularly if the user is displaying two pages per screen.

    The range command works from the temporay image in cache memory of your actual file and that's what makes it faster and less prone to error ... it doesn't have to interact with the screen driver.

    In the end, your question was answered and that's what's important.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-08-04T21:32:52+00:00

    Im not quite sure what this does, but it does not replace the bold text with my bold character style. Thanks for answering, though.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-08-04T21:31:59+00:00

    Thanks, this does the trick. I have no idea why I couldn't simply record this, but the process always returned the original formula I sent ????? Does it have something to do with having to use the mouse to set the font and style properties in the advanced find and replace box? There is no way to set everything using the keyboard -- the extended dialog box cannot be opened via keyboard. Thank you again, the example taught me a lot.

    Jay

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-08-04T20:51:20+00:00

    Try this:

    Sub ChgBold()

        Dim doc As Word.Document

        Dim rng As Word.Range

        Set doc = Word.ActiveDocument

        For Each rng In doc.Words

            If rng.Bold = True Then

                Debug.Print rng.Text

            End If

        Next

    End Sub

    And the developer reference to use is Office 2007

    http://msdn.microsoft.com/en-us/library/bb244515(v=office.12).aspx

    Was this answer helpful?

    0 comments No comments