View Single Post
 
Old 10-05-2021, 11:36 AM
yogis4031 yogis4031 is offline Windows 10 Office 2019
Novice
 
Join Date: Oct 2021
Posts: 1
yogis4031 is on a distinguished road
Default Replace the layout of a regexp

Hello all,


I've written a macro to replace and update a keyword starting with '#(\d\w)' in a word document but my code seems not working.


Nothing is updated.
Do you have any advice or clue to fix it ?

Sub UpdateData()
Dim RegexObject As RegExp
Dim RegexMatches As MatchCollection
Dim RegexMatch As Match

' Create an object RegExp
Set RegexObject = New RegExp

' Customize the RegExp object
With RegexObject
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "#(\d\w)+"
Set RegexMatches = .Execute(Selection.Text)
End With
' Search all keywords
With Selection.Find
.ClearFormatting
.Forward = True
.Format = False
.MatchCase = True

For Each RegexMatch In RegexMatches
.Text = RegexMatch
.Execute

' Select the keyword and update the color
With Selection
.Font.Color = wdColorPink
End With
Next RegexMatch
End With

End Sub
Reply With Quote