Programmed (VBA) mail merge not working in Office 2007



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


.



Relevant Pages

  • Re: access 2003
    ... Dim ctl As Control ... Dim rs As Recordset ... This sets the query definitions for choosing data to create an invoice using ... Event on combo box: Private Sub ChooseCust_AfterUpdate ...
    (microsoft.public.access.conversion)
  • Re: A simple problem with MoveFirst
    ... update the table (rather than using the recordset) means that from ADO's ... Dim tbDataToBeEmailed As ADODB.Recordset ... tbDataToBeEmailed.Open "tbDataToBeEmailed", cnCurrent, adOpenKeyset, ... "You have not entered time into the ACIS ...
    (microsoft.public.access.modulesdaovba)
  • Re: macro error due to editor
    ... Your VBA code never sets DB or the Recordset variables to Nothing. ... Do you declare DB as a global variable anywhere in the database file (e.g., ... EmpDateAs String ... Dim DB As Database, Qry As QueryDef, Qry_def As String ...
    (microsoft.public.access.macros)
  • Re: DAO MUCH faster than ADO in this test
    ... DAO is well-known to be faster than ADO. ... Of course the DAO loop ran faster than the SQL loop; ... advantage of a table-type recordset, which only works on local tables. ... Dim starttime As Single, finishtime As Single ...
    (microsoft.public.access.modulesdaovba)
  • Re: WillChangeRecord-Ereignis
    ... >> Dim WithEvents rs As ADODB.Recordset ... >> End With ... > Du hast Deinen Textboxen ein Recordset als Datasource zugewiesen. ... > Datensatz ein implizites Recordset.Update mit Übernahme der geänderten ...
    (microsoft.public.de.vb.datenbank)