![]() |
#1
|
|||
|
|||
![]()
Hi
Thanks! |
#2
|
||||
|
||||
![]()
You could use a wildcard Find expression like:
strSearch = "\@[A-Z]@_[0-9]{2}"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you Paul, works fine.
Now, what is the pattern to fully match @PART_01 and @ERTDRETHH ? In other words, I'm looking for the syntax to make the last 3 characters "_01" optional. The 3rd character from last (if present) is always an underscore and the next 2 are numbers. Thanks again. |
#4
|
|||
|
|||
![]()
Hi! Unfortunately, you can't use a one-line regex to do this. Try the following, which will work on the selecton, not on the entire doc (it's my personal preference):
Code:
Sub Find_N_Embolden() Dim rng As range Dim FindTxt As String Application.ScreenUpdating = False Set rng = Selection.range FindTxt = "\@[A-Z]{1,}" Do With rng.Find .text = FindTxt .MatchWildcards = True .Forward = True .Wrap = wdFindStop .Execute End With If rng.Find.found And rng.InRange(Selection.range) Then If rng.Characters.Last.Next = "_" And _ rng.Characters.Last.Next.Next Like "[0-9]" And _ rng.Characters.Last.Next.Next.Next Like "[0-9]" And _ Not rng.Characters.Last.Next.Next.Next.Next Like "[A-Za-z0-9]" Then rng.MoveEnd unit:=wdCharacter, count:=3 rng.Font.Bold = True: GoTo lbl_Next ElseIf rng.Characters.Last.Next <> "_" Then rng.Font.Bold = True Else: GoTo lbl_Next End If Else Exit Do End If lbl_Next: rng.Collapse wdCollapseEnd Loop Application.ScreenUpdating = True Set rng = Nothing End Sub Last edited by vivka; 07-23-2025 at 10:12 PM. |
#5
|
||||
|
||||
![]() Quote:
strSearch = "\@[A-Z]{2}[A-Z_0-9]{1,}" but you may get false matches
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Thank you for this. Not tested yet but this workaround makes sense given the limitations of Word's regex.
|
#7
|
|||
|
|||
![]()
You are welocme! The code seems to work correctly.
|
#8
|
|||
|
|||
![]()
Vivka,
Question and comment about your code below. 1. I notice that you (and others I have observed) use the Find.Found method. E.g. If rng.find.found ... Why? find.found returns True but so does If .Execute. 2. Your code will error if the one of the find returns is at the end of the document. Consider: Code:
Sub Find_N_Embolden() 'Revised GKM Dim oRng As Range Application.ScreenUpdating = False Set oRng = Selection.Range With oRng.Find .Text = "\@[A-Z]{1,}" .MatchWildcards = True .Forward = True .Wrap = wdFindStop Do While .Execute If oRng.InRange(Selection.Range) Then Select Case True Case oRng.End = ActiveDocument.Range.End - 1 oRng.Font.Bold = True Case oRng.Characters.Last.Next <> "_" oRng.Font.Bold = True Case oRng.End <= ActiveDocument.Range.End - 4 oRng.MoveEnd unit:=wdCharacter, Count:=3 If Right(oRng.Text, 3) Like "_[0-9][0-9]" Then oRng.Font.Bold = True End Select End If oRng.Collapse wdCollapseEnd Loop End With Application.ScreenUpdating = True Set oRng = Nothing End Sub Quote:
|
#9
|
|||
|
|||
![]()
Thank you, Greg, for your remarks! I fully accept them. My final code taking advantage of your suggestions (If conditions can be replaced wih Case conditions):
Code:
Sub Find_N_Embolden() 'In selection, embolden all '@AnyNumberOfCapitals' or ''@AnyNumberOfCapitals_DigitDigitNotFollowedByDigitOrLetter'. Dim oRng As range Dim FindTxt As String Application.ScreenUpdating = False Set oRng = Selection.range FindTxt = "\@[A-Z]{1,}" Do With oRng.Find .text = FindTxt .MatchWildcards = True .Forward = True .Wrap = wdFindStop If .Execute And oRng.InRange(Selection.range) Then If oRng.End >= ActiveDocument.range.End - 4 Then oRng.Font.Bold = True: GoTo lbl_Next ElseIf oRng.Characters.Last.Next <> "_" Then oRng.Font.Bold = True: GoTo lbl_Next ElseIf Right(ActiveDocument.range(oRng.start, oRng.End + 4), 4) Like "_[0-9][0-9][!A-Za-z0-9]" Then oRng.MoveEnd unit:=wdCharacter, count:=3 oRng.Font.Bold = True: GoTo lbl_Next Else: GoTo lbl_Next End If Else Exit Do End If End With lbl_Next: oRng.Collapse wdCollapseEnd Loop Application.ScreenUpdating = True Set oRng = Nothing End Sub Last edited by vivka; 07-31-2025 at 06:59 AM. |
![]() |
Tags |
regex, vba |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
VBAFiddler | Word VBA | 11 | 02-13-2021 01:16 AM |
![]() |
LQuinn | Word VBA | 2 | 01-28-2021 11:57 PM |
Is it possible to use wild cards or regular expression in autocorrect | kcvinu | Word | 2 | 12-14-2018 02:06 PM |
![]() |
qubie | Word | 2 | 11-29-2017 05:48 AM |
VBA code to enable Regular Expression | ronmayerle | Word VBA | 2 | 11-20-2014 01:09 PM |