Using VBScript Regex in Word VBA

P

Poseur

Someone might find this handy.
Having gotten comfortable using the VBScript RegExp Object via
COM in Excel, I wanted to use it also in Word just as a handy
little precise and efficient search and replace utility. Could
not find any suggestions on how to do that exactly. Googling
these groups, I saw a lot of references to Graham Mayor's
article "Finding and Replacing Characters Using Wildcards"
(http://www.mvps.org/word/FAQs/General/UsingWildcards.htm) but
after I read it it seemed I'd have to learn 2 sets of meta-
characters, a different behavior (non-greedy by default) and
the wildcard set does not offer any lookaround facility.
So, I piddled and I finally got it. An interactive fxn
(procedure) I put in my custom menu. It is very quick and I
can be very precise:

For a Selection:
Sub RegExInterSel()
Dim regEx As VBScript_RegExp_55.RegExp
Dim withWhat As String
Dim newStr As String
Set regEx = New VBScript_RegExp_55.RegExp
With regEx
.Pattern = InputBox("Replace What?")
.Global = True
.IgnoreCase = False
End With
withWhat = InputBox("With What?")
If regEx.Test(Selection.Text) Then
Selection.Text = regEx.Replace(Selection.Text,withWhat)
End If
End Sub

For the whole document:
Sub RegExInterAll()
Dim regEx As VBScript_RegExp_55.RegExp
Dim withWhat As String
Dim Sentence As Range
Set regEx = New VBScript_RegExp_55.RegExp
With regEx
.Pattern = InputBox("Replace What?")
.Global = True
.IgnoreCase = False
End With
withWhat = InputBox("With What?")
For Each Sentence In ActiveDocument.Range.Sentences
If regEx.Test(Sentence.Text) Then
Sentence.Text = regEx.Replace(Sentence.Text, _
withWhat)
End If
Next Sentence
End Sub

There are probly some inefficiences in the above, particular
the latter using the Range object. Had trouble with that one.
If I "replaced" using large Range text properties for the
entire document, all formatting went away and I got a text
file. This works.
 
W

Word Heretic

G'day Poseur <[email protected]>,

Have you done timer comparisons vs Selection.Find?

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Poseur reckoned:
 
P

Poseur

G'day Poseur <[email protected]>,

Have you done timer comparisons vs Selection.Find?

Sir Heretic,

Don't even know how. I am an authentic Poseur, truly not the
real deal. When I say it is fast, I mean simply that for my
purposes and relatively small documents, it is as quick as I
need. And it is faster than the built-in "wildcards" system
because I already know the regex metas and syntax and don't have
to think in a different system.
Do have one correction to my original. The VBScript regex object
syntax does not include look-arounds (my bad). .NET has a rich
regex object on a par with Perl but requires access to the .NET
objects.
I'm trying to figure out how to wrap the open source PCRE (Perl
Compatible Regular Expressions)as a COM object - anyone have any
ideas, let me know
 
W

Word Heretic

G'day Poseur <[email protected]>,

StartTime = TimeValue(Now)
.....
ElapsedTime = TimeValue(Now) - StartTime



Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Poseur reckoned:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top