Find/Replace MS Word with Mail Merge Fields

Hey Experts,

I have a bunch of mail templates, in MS Word, that I need to change all the mail merge fields to different values b/c we're using new software. I've found some good Word Find/Replace programs, but they don't replace the strings w/ Fields. Best case, I would like to be able to read all the old/new values I want to process from a text file or excel spreadsheet or database or whatever and search all the Word docs in a specified directory(including subdirectories) and change values w/ those special fields. I assume this can be done in VB. Any help would be greatly appreciated.
Thanks in advance.

The following code isn't a complete solution, but it might get you moving in the right direction.
This is a VB program that opens a document and replaces merge fields.
(You need to add a reference to Microsoft Word Object Library.)

You could modify this example by passing in the document to open, and a collection old and new fields.

Private Sub Command1_Click()
Dim word As word.Application
Dim field As String
Dim lastpos As Long

What you are doing is called "Parsing" to find the value that you want to change. Then you change it and rewrite the file.

If you post a sample of the data I will try and write a REGEX for you.

Then you can simply use a system() call from VB to execute the PERL script.

What it will do is open a file, parse, store the NEW values into an array, close the file, open the text file, parse, locate the OLD value, replace with the NEW values in the array, write text file, close.

Yes, but this isn't a text file. Text files are easy; this is a Word Document. I'm pretty sure I have to do something like SCDMETA. I just can't find alot of documentation on this. And I don't have VS.NET, so I'm compiling from the command line, and I'm not sure how to reference the Word Object Libary. It's keeps telling me "'Word.Application' is undefined". Does anybody know which .dll this is contained? Thanks ahead.

Word.Application is a COM object exposed by the WinWord.exe.
Typically with COM you don't have to know where the binary is located. You can access the functionality by instanciating the COM object through the progID "Word.Application" or the GUID (Global Unique Identifier).

In VB6, you could either add a reference the Word.Application at design time and VB could early bind to the COM object.
OR you could not reference the Word.Application at design time, instead Dim X as object, then set X = CreateObject("Word.Application").

I don't know the equivalent to .NET.

As a side note: most of the code I posted was generated from recording a word macro. I then converted to run in VB6. I didn't realize you were running VB.NET.