Finding in Word with Regular Expressions

E

Evan Stone

Is is possible to perform a Find operation (and by extension, a replace) in
Word with Regular Expressions using automation, and if so, how does one go
about doing it? Is it similar to the standard Find methodology?

....and I'm asking this from a C# perspective.

Thanks!

evan stone | software engineer
 
C

Charu Tevari

Hmm, It's surprising that when we call Find while defining a word macro, the
dialog does not present the option of using regular expressions so doing
striaght regex search/replace automation may not be possible.

I suppose you could always use brute force by operating on a string
consisting of the range for the entire document:

String wholeDocStr = theDoc.range.text;
<do the regex stuff here transforming wholeDocStr >
theDoc.range.text = wholeDocStr;


-Charu
 
K

Kalpesh

Hi,

Word does allow find/replace based on regular expressions
You will have to use word object model to do such a thing

For eg
my document has the following text
Ab1a
Ab2b
Ab3c
Ab4d
Ab5e
Ab51f

The following code will search for the text based on the pattern
Here is the code

<code>
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Ab[0-9][a-c]"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
</code>

This will match only the first 3 lines (matching with the find pattern)
So, you will have to use interop to word object model.

Does this help ?

Kalpesh
 

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