Consulting

Results 1 to 8 of 8

Thread: Need Help with Find and Replace Code in Word for text within Parenthesis

  1. #1
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location

    Need Help with Find and Replace Code in Word for text within Parenthesis

    Hi all,

    I would like to bold text within parentheses to also include the text to be highlighted in red.

    I found a couple of options to do this but it doesn't work at all. Don't know if the problem is with the Find option or the Replace with option.

    Probably both are wrong.

    Thank you in advance for your assistance.





    Sub Macro2()
    '
    ' Macro to highlight text with parenthesis
    
    
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "\(*\)"
            .Replacement.Text = "\1"
            .Forward = True
            .Wrap = wdFindContinue
            .Font.Bold = True
            .Font.ColorIndex = wdRed
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

  2. #2
    VBAX Regular
    Joined
    Jan 2022
    Posts
    16
    Location
    Hi! One point to clarify, if you don't mind: should the macro find red txt in the parenthesis & then bold it or should it find any txt in the parenthesis & then color it red & bold it?
    If the first condition, use:
    Sub Bold_Parentheses_If()
    'Bold red strings in parentheses in the selected range.
    
    Dim rng As range
    
        Application.ScreenUpdating = False
        Set rng = selection.range
        With rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = "\(*\)"
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = True
            Do While .Execute And rng.InRange(selection.range)
                rng.Start = rng.Start + 1
                rng.End = rng.End - 1
                If rng.Font.ColorIndex = wdRed Then
                    rng.Font.Bold = True
                End If
                rng.Collapse wdCollapseEnd
             Loop
        End With
    Application.ScreenUpdating = False
    Set rng = Nothing
    End Sub
    If the 2nd condition, delete 'If', 'Then' & 'End If'.
    Last edited by Vladimir; 01-21-2024 at 09:31 AM.

  3. #3
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    Hi Vladimir,

    Thank you for the code. My idea was to select the second option but I tried both options but for some unknown reason it's still not working.

    If you can attach a copy of the file after running the macro in your end that will show me that is working if that is not a problem.

    Thanks!

  4. #4
    VBAX Regular
    Joined
    Jan 2022
    Posts
    16
    Location
    Hi again, rsrasc! I've tested & retested the code and it works properly for me. Some notes: before running the macro, you should select some part of your txt. If you want to process the whole doc, press Ctrl+A. As for attaching a sample file, I don't think it's really needed, just copy-paste the following txt in your doc & run the macro:
    Test (test) test (test)
    Test (test) test (test)
    Test (test) test (test)
    Test (test) test (test)
    Test (test) test (test)

    And the code itself:
    Sub Bold_Parentheses_If()
    'Bold & color red the strings in parentheses in the selected range.
    
    Dim rng As range
    
        Application.ScreenUpdating = False
        Set rng = selection.range
        With rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = "\(*\)"
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = True
            Do While .Execute And rng.InRange(selection.range)
                rng.Start = rng.Start + 1
                rng.End = rng.End - 1
                rng.Font.ColorIndex = wdRed
                rng.Font.Bold = True
                rng.Collapse wdCollapseEnd
             Loop
        End With
    Application.ScreenUpdating = False
    Set rng = Nothing
    End Sub

  5. #5
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    Hello Vladimir, thanks for your help. It's working now but only when I select Ctrl + A.

    Thank you for taking your time.

    Much appreciated.

  6. #6
    VBAX Regular
    Joined
    Jan 2022
    Posts
    16
    Location
    You are welcome, rsrasc. My last note: I prefer selection to ActiveDocument. But if you want to avoid pressing Ctrl+A, change 'rng = selection.range' to 'rng = ActiveDocument.range'.

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    My apologies for coming in late here, but I was trying to remember where I had seen something familiar with this code. Does this work for you?

    Sub BoldParen()
    With ActiveDocument.Content.Find 
        .ClearFormatting
        .Text = "(\(*)"
        .Font.Bold = False 
        With .Replacement 
            .ClearFormatting 
            .Font.Bold = True
            .Font.ColorIndex = wdRed 
        End With 
        .Execute FindText:="", ReplaceWith:="", Format:=True, Replace:=wdReplaceAll 
    End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Regular
    Joined
    Jan 2022
    Posts
    16
    Location
    Hi! Sure, Aussibear, your code works perfectly! My first code was alike. But after I had seen rsrasc's sample doc, I had to include looping, because round brackets needed to be excluded form reformatting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •