Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2023, 06:13 AM
alex100 alex100 is offline Search & replace: capture wildcard groups and set a bold formatting Windows 7 64bit Search & replace: capture wildcard groups and set a bold formatting Office 2016
Advanced Beginner
Search & replace: capture wildcard groups and set a bold formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Search & replace: capture wildcard groups and set a bold formatting

I would like to replace some keywords using wildcards, while also capturing the wildcard group and applying bold formatting to the matches. I have two codes so far, but neither one accomplishes both requirements.



The first code captures the group but does not set the bold formatting:

Code:
With Selection.Find
    .Text = "(wildcard_pattern_here)"
    .Replacement.Text = "original text: \1"
    .Replacement.Font.Bold = True
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
The second code does the exact opposite - applies the bold formatting but does not insert captured groups in the replacement text:

Code:
With Selection.Find
    .Text = "(wildcard_pattern_here)"
    .Replacement.Text = "original text: \1"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do While .Execute
        Selection.Font.Bold = True
        Selection.TypeText Text:=.Replacement.Text
    Loop
End With
How can I achieve both requirments at the same time, please?

Thank you!

Alex
Reply With Quote
  #2  
Old 02-24-2023, 06:58 AM
gmayor's Avatar
gmayor gmayor is offline Search & replace: capture wildcard groups and set a bold formatting Windows 10 Search & replace: capture wildcard groups and set a bold formatting Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Code:
With Selection.Find
    .Text = "((wildcard_pattern_here)"
    .Replacement.Text = "original text: \1"
    .Replacement.Font.Bold = True
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-24-2023, 08:45 AM
alex100 alex100 is offline Search & replace: capture wildcard groups and set a bold formatting Windows 7 64bit Search & replace: capture wildcard groups and set a bold formatting Office 2016
Advanced Beginner
Search & replace: capture wildcard groups and set a bold formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

All right, so the '.Format' setting was the one preventing the style change.

Thank you, it works great now!

Alex
Reply With Quote
  #4  
Old 02-24-2023, 09:21 AM
alex100 alex100 is offline Search & replace: capture wildcard groups and set a bold formatting Windows 7 64bit Search & replace: capture wildcard groups and set a bold formatting Office 2016
Advanced Beginner
Search & replace: capture wildcard groups and set a bold formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

I would not want to open another thread just for this, so I have one more related question if you do not mind...

This RegEx matches one to ten uppercase words, divided by a space character and ending with a colon and space.

Code:
.Text = "([A-Z]@ ){1,10}[A-Z]@: "
.Replacement.Text = "> \1"
It works good as it is listed above, but when I try to enclose it between two parentheses (to capture the group), I get a 'not valid pattern expression' error. The code is this:

Code:
.Text = "(([A-Z]@ ){1,10}[A-Z]@: )"
.Replacement.Text = "> \1"
How should I write the wildcard pattern, please?

Alex
Reply With Quote
  #5  
Old 02-24-2023, 01:55 PM
alex100 alex100 is offline Search & replace: capture wildcard groups and set a bold formatting Windows 7 64bit Search & replace: capture wildcard groups and set a bold formatting Office 2016
Advanced Beginner
Search & replace: capture wildcard groups and set a bold formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Ok, problem solved, I changed the pattern with the one below and it works good now.

Code:
.Text = vbCr & "([A-Z ]{1,50}): "
.Replacement.Text = "> \1"
Alex
Reply With Quote
  #6  
Old 02-24-2023, 09:39 PM
gmayor's Avatar
gmayor gmayor is offline Search & replace: capture wildcard groups and set a bold formatting Windows 10 Search & replace: capture wildcard groups and set a bold formatting Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

See Replace using wildcards
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard find/replace wildcard question hephalumph Word 1 03-12-2022 12:54 AM
wildcard search and add (not replace) sequential numbers in front equalizer88 Word VBA 4 05-07-2016 08:04 PM
Search & replace: capture wildcard groups and set a bold formatting Find/Replace Wildcard Needed-Bold & Highlight rsrasc Word VBA 3 11-11-2014 03:55 PM
Issue with Wildcard Search and Replace linusthedog Word 3 09-04-2014 03:32 PM
how to search and replace BOLD text >> font color change? dylansmith Word 4 03-12-2013 09:51 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:02 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft