Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using VBScript Regex in Word VBA

634 views
Skip to first unread message

Poseur

unread,
Aug 21, 2004, 10:11:03 PM8/21/04
to
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.

--
I'm gonna let you go now.

Poseur
Be whoever you want to be...it's the 'net!

Word Heretic

unread,
Aug 27, 2004, 12:38:01 AM8/27/04
to
G'day Poseur <julioYODUD...@yahoo.com>,

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:

Poseur

unread,
Aug 29, 2004, 8:35:23 PM8/29/04
to
Word Heretic <myful...@tpg.com.au> wrote in
news:mketi099smhlid6hd...@4ax.com:

> G'day Poseur <julioYODUD...@yahoo.com>,
>
> 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

Word Heretic

unread,
Aug 29, 2004, 9:39:31 PM8/29/04
to
G'day Poseur <julioYODUD...@yahoo.com>,

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:

>Word Heretic <myful...@tpg.com.au> wrote in

Péter Tófalvi

unread,
Mar 6, 2023, 6:05:46 PM3/6/23
to
> If I "replaced" using large Range text properties for the
> entire document, all formatting went away and I got a text
> file. This works.

OK, so actually this doesn't work. Losing all your formatting plus all embedded photos, styles etc. is a MAJOR problem.
I am thinking about finding a solution to this issue, but I am still at the beginning of the task.

P.
0 new messages