View Full Version : [SOLVED:] Need Help with Find and Replace Code in Word for text within Parenthesis
rsrasc
01-21-2024, 04:30 AM
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
Vladimir
01-21-2024, 06:25 AM
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'.
rsrasc
01-21-2024, 08:36 AM
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!
Vladimir
01-21-2024, 09:23 AM
Hi again, rsrasc (http://www.vbaexpress.com/forum/member.php?39298-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
rsrasc
01-21-2024, 10:07 AM
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.
Vladimir
01-21-2024, 01:11 PM
You are welcome, rsrasc (http://www.vbaexpress.com/forum/member.php?39298-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'.
Aussiebear
01-21-2024, 06:08 PM
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
Vladimir
01-22-2024, 08:35 AM
Hi! Sure, Aussibear, your code works perfectly! My first code was alike. But after I had seen rsrasc (http://www.vbaexpress.com/forum/member.php?39298-rsrasc)'s sample doc, I had to include looping, because round brackets needed to be excluded form reformatting.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.