macro to find and replace in batch from a MS 2007 Word file using Regular Expressions (regex)

Hello everyone.

In a .docx file, I would like to replace in batch groups of several original words by just another single term which must appear in the same formatting as the original terms.
Since the in MS Word the Find and Replace option does not support complex regex such as abc(?!(cde\b|ba)), and I need to carry out dozens of such time-consuming changes involving sometimes just parts of words, I thought a macro supporting regular expressions could somehow do the job.

Note that you'll need to add whatever document processing code you want, where indicated. To use the full power of Regular Expression functions in Word, you'd need to use a macro with a refererence to the Microsoft vbScript Regular Expressions library. Whilst Word's own wildcard support only gives access to a sub-set of the Regular Expression functions, it does provide functionality the vbScript Regular Expressions library doesn't support, including the ability to specify character/paragraph formatting and languages - for both the find & replace side of things.

I am a newbie, so I do not really understand the 'syntax' appearing in the macro. I'd really appreciate it if you were as kind as to explain in broad terms the different chunks of it.

an Excel workbook to hold the Find expressions in one column and the Replace expressions in another

I do not know where in the macro the path of this workbook must be indicated.

add whatever document processing code you want, where indicated

I am afraid I do not know what document processing code is and where it's supposed to belong.

you'd need to use a macro with a refererence to the Microsoft vbScript Regular Expressions library

If the macro you posted does not support Microsoft vbScript Regular Expressions library, then I'd like to know how such reference is to be made.

Word's own wildcard support only gives access to a sub-set of the Regular Expression functions

I'd like to know if it's possible for some lines of the macro to be changed so that it supports alternation, when specified, between such sub-set of wildcards and the Microsoft vbScript Regular Expressions library.

I am sorry for my lack of proper specific terminology.
I gratefully hope to receive a prompt reply.

I am a newbie, so I do not really understand the 'syntax' appearing in the macro. I'd really appreciate it if you were as kind as to explain in broad terms the different chunks of it.

I had expected anyone saying they wanted to use the full Regular Expression functionality and providing an MSDN link for its usage with Visual Studio would have more than a passing knowledge of VBA.

Originally Posted by REGEX

I do not know where in the macro the path of this workbook must be indicated.

As coded, the macro presupposes the Find/Replace expressions would be on 'Sheet1' of a workbook named 'Workbook Name.xls' in the user's 'Documents' folder. All of that is encompassed in just two lines:
StrWkBkNm = "C:\Users\" & Environ("Username") & "\Documents\Workbook Name.xls"
StrWkSht = "Sheet1"

Originally Posted by REGEX

I am afraid I do not know what document processing code is and where it's supposed to belong.

If you read the code, you'll see the comment line:
'code to process the document goes here

Originally Posted by REGEX

If the macro you posted does not support Microsoft vbScript Regular Expressions library, then I'd like to know how such reference is to be made.

The macro does support it, but you must add the library reference, via Tools|References in the VBE

Originally Posted by REGEX

I'd like to know if it's possible for some lines of the macro to be changed so that it supports alternation, when specified, between such sub-set of wildcards and the Microsoft vbScript Regular Expressions library.

Yes, that's possible. However, since I have no idea what you want to do and no first-hand experience using the Microsoft vbScript Regular Expressions library, I can't really help you on that front.

I’ve played around using tools such as dnGREP or powerGREP, so I am aware of the regex power http://www.regular-expressions.info/. Still, I would very much like to get an overall understanding of the 'syntax' appearing in the different lines pertaining to the different parts of the macro

According to the number of ampersands, there seem to be three different paths to fill, namely a) "C:\Users\" b) Environ("Username") c) "\Documents\Workbook Name.xls", so I am not sure exactly up to what extend the .docx file path should be jotted down.

'code to process the document goes here

Then I supposse I must delete this whole line and instead paste a ‘code’; yet again, I do not know what exactly must be added, i.e., what ‘code’ actually signifies.

you must add the library reference, via Tools|References in the VBE

Should I add just the 5.5 version, or also the 1.0 as well?

I have no idea what you want to do

I’d really appreciate it if you could tell me what I might add to the post to clarify my issue.

According to the number of ampersands, there seem to be three different paths to fill, namely a) "C:\Users\" b) Environ("Username") c) "\Documents\Workbook Name.xls", so I am not sure exactly up to what extend the .docx file path should be jotted down.

Actually the & is the string join feature in VBA so the statement is taking C:\Users\" then joining/appending the Environment Variable Environ("Username") then joining/appending the [/noparse]"\Documents Workbook Name.xls"[/noparse]. Thus the only part you would need to change is the Workbook Name with the actual name of your workbook which you would locate in your Documents folder.

That you've needed to ask the basic questions you've asked so far indicates you have little practical knowledge of VBA or Regular Expressions. I'd recommend starting by learning how to use VBA, at least, before trying to tackle your own programming. Likewise, I don't think we want to turn this thread into an extended VBA/Regular Expressions tutorial.

I don't think we want to turn this thread into an extended VBA/Regular Expressions tutorial.

O.k.

I'd like to test the macro as soon as possible, but I still hesitate regarding

1) StrWkBkNm = "C:\Users\" & Environ("Username") & "\Documents\Workbook Name.xls"
since I do not store files in the default 'Documents' folder, but in E:\, I'd like to know, if possible, how I can modify such line so that I can just paste the file path as such.

2) 'code to process the document goes here
what I am supposed to replace the above line with

Lastly, I'd like to know whether there are any more lines that I am to fill out.

1) StrWkBkNm = "C:\Users\" & Environ("Username") & "\Documents\Workbook Name.xls"
since I do not store files in the default 'Documents' folder, but in E:\, I'd like to know, if possible, how I can modify such line so that I can just paste the file path as such.

So replace as much as you need after the '=' character with your actual file path & name.

Originally Posted by REGEX

2) 'code to process the document goes here
what I am supposed to replace the above line with

You replace it with whatever code you need. I've given you links showing what might go there.

Originally Posted by REGEX

Lastly, I'd like to know whether there are any more lines that I am to fill out.

That's entirely up to you. We have no idea what you need, since you haven't actually told us anything about what you're trying to achieve.

At first I had in mind implementing conditional regular expressions both in the Find column and in the Replace column of the Excel workbook; yet, I've just read that the Microsoft vbScript does not support, among other regex, such conditionals, unlike for example The Microsoft .NET Framework.
Therefore, I’d like to know if it would be possible to adjust the macro so that it also runs such kind of conditionals on both columns.

You replace it with whatever code you need. I've given you links showing what might go there.

In neither link can I find an expression similar to 'code to process the document goes here. but rather the word 'code' is used as a synonym for what I call a macro. code.PNG

So honestly, I still do not know what type of data I am to replace that line with.

At first I had in mind implementing conditional regular expressions both in the Find column and in the Replace column of the Excel workbook; yet, I've just read that the Microsoft vbScript does not support, among other regex, such conditionals, unlike for example The Microsoft .NET Framework.
Therefore, I’d like to know if it would be possible to adjust the macro so that it also runs such kind of conditionals on both columns.

I have already told you:

I have no idea what you want to do and no first-hand experience using the Microsoft vbScript Regular Expressions library, I can't really help you on that front.

I'm not about to start playing guessing games.

Originally Posted by REGEX

In neither link can I find an expression similar to 'code to process the document goes here. but rather the word 'code' is used as a synonym for what I call a macro.
...
So honestly, I still do not know what type of data I am to replace that line with.

Well, what do you suppose a comment line like:
'Process each word from the F/R List
which appears in both links, followed by a block of code that processes the document's content, might suggest???

I'm not prepared to waste any more time on this. All you've done so far is to ask for code to implement a particular process, when we have no idea what the objective is and the process you've specified may not even be appropriate to the task. If you want to play that game, you won't find any takers, here or on any other forum.