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\sch ool 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(folde r & template, "Word.Document" )
' Make Word visible.
objWord.Applica tion.Visible = True
objWord.MailMer ge.OpenDataSour ce _
Name:=folder & database, _
LinkToSource:=T rue, _
Connection:="TA BLE Test", _
SQLStatement:=" SELECT * FROM [Test]"
' Execute the mail merge.
objWord.MailMer ge.Destination = wdSendToNewDocu ment
objWord.MailMer ge.Execute
' Save the document
objWord.SaveAs FileName:=folde r & "7.doc",
FileFormat:=wdF ormatDocument, _
LockComments:=F alse, Password:="", AddToRecentFile s:=True,
WritePassword _
:="", ReadOnlyRecomme nded:=False, EmbedTrueTypeFo nts:=False, _
SaveNativePictu reFormat:=False , SaveFormsData:= False,
SaveAsAOCELette r:= _
False
' Release the object variable.
objWord.Close
Set objWord = Nothing
End Sub
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\sch ool 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(folde r & template, "Word.Document" )
' Make Word visible.
objWord.Applica tion.Visible = True
objWord.MailMer ge.OpenDataSour ce _
Name:=folder & database, _
LinkToSource:=T rue, _
Connection:="TA BLE Test", _
SQLStatement:=" SELECT * FROM [Test]"
' Execute the mail merge.
objWord.MailMer ge.Destination = wdSendToNewDocu ment
objWord.MailMer ge.Execute
' Save the document
objWord.SaveAs FileName:=folde r & "7.doc",
FileFormat:=wdF ormatDocument, _
LockComments:=F alse, Password:="", AddToRecentFile s:=True,
WritePassword _
:="", ReadOnlyRecomme nded:=False, EmbedTrueTypeFo nts:=False, _
SaveNativePictu reFormat:=False , SaveFormsData:= False,
SaveAsAOCELette r:= _
False
' Release the object variable.
objWord.Close
Set objWord = Nothing
End Sub
Comment