Microsoft Office Forums


Reply
Thread Tools Display Modes

Using VBScript Regex in Word VBA

 
 
Poseur
Guest
Posts: n/a
 
      08-22-2004, 02:11 AM
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/Genera...gWildcards.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!
 
Reply With Quote
 
 
 
 
Word Heretic
Guest
Posts: n/a
 
      08-27-2004, 04:38 AM
G'day Poseur <(E-Mail Removed)>,

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:

>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/Genera...gWildcards.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.


 
Reply With Quote
 
 
 
 
Poseur
Guest
Posts: n/a
 
      08-30-2004, 12:35 AM
Word Heretic <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> G'day Poseur <(E-Mail Removed)>,
>
> 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

--
I'm gonna let you go now.

Poseur
Be whoever you want to be...it's the 'net!
 
Reply With Quote
 
Word Heretic
Guest
Posts: n/a
 
      08-30-2004, 01:39 AM
G'day Poseur <(E-Mail Removed)>,

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 <(E-Mail Removed)> wrote in
>news:(E-Mail Removed) :
>
>> G'day Poseur <(E-Mail Removed)>,
>>
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a VBA RegEx question Geoff Cox PowerPoint Newsgroup 4 08-16-2006 08:26 PM
Symbol font chars changing to "(" after passing through VBA Regex jalanford Word VBA 6 07-31-2006 11:14 PM
Need help accessing the matches collection returned vbscript regex Dave Jenkins PowerPoint Newsgroup 2 10-14-2005 06:32 PM
Using VBScript Regex in Word VBA Poseur Word VBA 3 08-30-2004 01:39 AM
sing VBScript Regex in Word VBA Poseur Word VBA 0 08-22-2004 05:20 PM


All times are GMT. The time now is 03:08 AM.
Microsoft Office Forums is not affiliated with Microsoft Corporation.


Welcome!
Welcome to the Microsoft Office Forums
 


Latest Threads
New user looking for shapes/templates
SargeUSMC (04-21-2014, 04:23 PM)

Mail Merging Content With High Quality
oleander (04-17-2014, 11:59 PM)

Aargh. What is this?
PeteJ (04-17-2014, 12:10 PM)

Word 2013 Insert Symbol (Not Responding)
tpthrshr (04-17-2014, 02:49 AM)

Can't save a large Word document
Josh (04-17-2014, 01:03 AM)