[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1406
  • Last Modified:

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.
0
matt_mcswain
Asked:
matt_mcswain
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now