View Single Post
 
Old 05-30-2023, 09:33 AM
hoosier122 hoosier122 is offline Windows 10 Office 2021
Novice
 
Join Date: May 2023
Posts: 3
hoosier122 is on a distinguished road
Default Help Creating a Simple Find & Replace with REGEX in Word

Greetings! I simply want to be able to use Regex within a VB script for Word.

Surely someone already has this script already written?

No, I don't want to use Word's built-in Find & Replace function including its "Wildcards" feature.

Code:
Sub LineCounter()
Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")

With regExp
    .Pattern = "(^p^p)"
    .Global = True
    Selection.Text = .Replace(Selection.Text, "^p")
End With
End Sub
The above code kind of works, but I don't want to have to highlight text AND this code also unpredictably breaks the formatting of Word documents in which it is run.

Help, please!
Reply With Quote