View Single Post
 
Old 05-31-2023, 04:06 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your code is working on Selection.Text which is why it expects you to SELECT the text first. If you don't want to first select the text then you need to ask it to work on a range eg
ActiveDocument.Range.Text

Also, you are telling the code to replace the Text (which is an unformatted string of text) with another unformatted string of text - this is why your code is removing the formatting.

Some examples of others use of Regex in VBA might give you some hints to fix your code
Include All Stories in VBA RegEx Search in MS Word - Stack Overflow
microsoft word - Use RegEx and wildcard search to find a pattern - Super User
Using VBScript Regex in Word VBA

Are you really using vbscript or are you using VBA? It appears to be VBA in your example.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote