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
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