I use MS Office 2003 and would like to run several Word mail merges
from Access.
My code so far is given below.
1. The code does not do what I want. I want to save the new document
with the merge result, not the merge template I start out with. At the
moment it saves the latter.
2. I would like all Word documents to be closed in the end.
3. I would like to run several queries and for each of them do a mail
merge to a new document that is being saved.
Any help with 1-3 would be most welcome.
Regards,
Jan Nordgreen
---
Option Compare Database
'References: Microsoft Word 11 Object Library
Private Sub Command0_Click()
' variables
Dim folder As String
folder = "G:\GIS\school administration\reports\"
Dim template As String
template = "report template.doc"
Dim database As String
database = "create report documents.mdb"
Dim objWord As Word.Document
Set objWord = GetObject(folder & template, "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:=folder & database, _
LinkToSource:=True, _
Connection:="TABLE Test", _
SQLStatement:="SELECT * FROM [Test]"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
' Save the document
objWord.SaveAs FileName:=folder & "7.doc",
FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False
' Release the object variable.
objWord.Close
Set objWord = Nothing
End Sub