Programmed (VBA) mail merge not working in Office 2007

M

Matt Colegrove

I had a small module that programatically performs a mail merge to create
single documents. The code worked fine in Office 2003, but now fails in
Office 2007. I know where it's failing, but can't figure out how to fix it.
The code runs in Access.

The module uses a RecordSet to drive the mail merge. For each record in the
recordset, a mailmerge is executed on the single record. The resulting
document is saved with a name derived from the data in the record set. The
place where the code now fails is on the line -
WordApp.Documents(1).SaveAs.... It used to be that the merged document was at
index (1) in the Documents collection. This appears to have changed in Word
2007. I've been scouring the documentation and have not been able to find
out how to get at the merged document. If anyone knows how I can access it,
I would be greatful.

The full code of this module is provided below:

Public Sub CreateWordMailMergeForInstall()

Dim WordApp As New Word.Application
Dim WordDoc As New Word.Document
Dim dbs As Database
Dim rs As Recordset
Dim RetValue As Integer
Dim SQL As String
Dim iRow As Integer
Dim Bldg, SerialNumber, SName, DevType As String

Const MergeDocPath As String = "\\Server\share\path to folder\"

iRow = 1
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("access query", dbOpenDynaset)

WordApp.Visible = False

Set WordDoc = GetObject("Z:\path to folder\mailmerge master
document.doc", "word.document")
WordDoc.Application.Visible = False

With rs
.MoveFirst
Do While rs.EOF = False
WordDoc.MailMerge.DataSource.FirstRecord = iRow
WordDoc.MailMerge.DataSource.LastRecord = iRow
WordDoc.MailMerge.Destination = wdSendToNewDocument
WordDoc.MailMerge.Execute

Select Case ![AssetTypeID]
Case 2
DevType = "DT"
Case 3
DevType = "LT"
Case Else
DevType = "UN"
End Select

Bldg = ![ClientBuildingNumber]
SerialNumber = ![PCSN]
SName = Left(![CASFirstName], 1) & ![CASLastName]
WordApp.Documents(1).SaveAs (MergeDocPath & Bldg & " Inst " &
DevType & " " & SerialNumber & " " & SName)
WordApp.Documents(1).Close

iRow = iRow + 1
.MoveNext
Loop
End With

rs.Close

Exit_CreateWordMailMergeForInstall_Error:
WordDoc.Close False
WordApp.Quit
Set WordApp = Nothing
Exit Sub

Err_CreateWordMailMergeForInstall_Error:
MsgBox Err.Description
Resume Exit_CreateWordMailMergeForInstall_Error

End Sub
 

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