Problem with VBA running a Word mail merge

R

Robert Bonds

Need a bit of help, please. Am trying to use VBA (in Microsoft Access) to run
the simplest kind of Word mail merge, but for the life of me I can't get the
syntax right. The merge template in Word is already set up with a fixed
tab-delimited text file as its source, so all that's needed is to run the
merge... but it won't go. Here's the procedure, along with the routine for
starting Word in case the problem lies there:

--------

Sub GenerateLetters()

Call GetWord
DoCmd.SetWarnings False
wd.Documents.Open "D:\myTemplate.doc"
DoCmd.SetWarnings True

With wd.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With

ActiveDocument.SaveAs "D:\myNewDocument.doc"
wd.Quit

--------

Sub GetWord()
On Error GoTo Error_GetWord

Set wd = GetObject(, "Word.Application")
Exit Sub

Error_GetWord:

If Err.Number = 429 Then
Set wd = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End If

End Sub

--------

The procedure hangs at the line ".Destination = wdSendToNewDocument", which
is no doubt why the code following that point seems to merely save the
template to the new location rather than actually merging to a new document
at the new location. But I can't figure out how/where to have the code
correctly specify the name and location of the new document to be created by
the merge.

Any help will be greatly appreciated. Thanks.

Robert Bonds
 
D

Doug Robbins - Word MVP

How is wd declared?

I would also declare wdDoc and use

Set wdDoc = wd.Documents.Open("D:\myTemplate.doc")

then use wdDoc in place of wd.ActiveDocument

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
R

Robert Bonds

Doug, Peter,

Thanks VERY much for your help. Looks like I needed to implement the
suggestions from both of you to get this to work. And now it does. Thanks
again.

Best,
Robert Bonds

-----------------------
 

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