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.
LVL 11
matt_mcswainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SCDMETACommented:
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
       
    Set word = New word.Application
    With word
        .Visible = True
        .Documents.Open """C:\Documents and Settings\TestField.doc""", False, False, False, "", ""
        .ActiveDocument.Activate
   
        .Selection.WholeStory
        .Selection.Fields.ToggleShowCodes
        .Selection.Fields.ToggleShowCodes
        .Selection.HomeKey Unit:=wdStory
        .Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
        .Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        If Asc(Mid$(.Selection.Text, 1, 1)) <> 171 Then
            .Selection.HomeKey Unit:=wdStory
            .Selection.WholeStory
            .Selection.Fields.ToggleShowCodes
        End If
        .Selection.HomeKey Unit:=wdStory
        .Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
        .Selection.GoTo What:=wdGoToField, Which:=wdGoToPrevious, Count:=1, Name:="MERGEFIELD"
        .Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        lastpos = .Selection.Start
        Do
            'Do Replace
            field = .Selection.Text
            If Asc(field) <> 13 Then
                field = Mid$(field, 2)
                field = Mid$(field, 1, Len(field) - 1)
                               
                Select Case field
                    Case "City"
                       .ActiveDocument.MailMerge.Fields.Add Range:=.Selection.Range, Name:="New_City"
                    Case "Last_Name"
                       .ActiveDocument.MailMerge.Fields.Add Range:=.Selection.Range, Name:="New_Last_Name"
                End Select
            End If
            lastpos = .Selection.Start
            .Selection.MoveRight Unit:=wdCharacter, Count:=1
            .Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
            .Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Loop While lastpos <> .Selection.Start
        .ActiveDocument.Save
    End With
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sapbucketCommented:
Use PERL instead.

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.

0
matt_mcswainAuthor Commented:
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.  
0
SCDMETACommented:
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.

0
matt_mcswainAuthor Commented:
I basically got this figured out. I'll give the points to SCDMETA.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.