Access to Word Mail Merge Automation
Hi all.
I was wondering if anyone can help?
I have been trying to automate a mailmerge from Access 2002 to Word 2002 (xp) but with no luck. I have a query that I wish to use as the datasource. I have found some code for the 97 version but can't get it to work in xp. I have tried the knowledge-base and have gone through this forum but am still struggling. I want to use a button to launch the mailmerge.
Does anyone have an example? Threads I have read seem to suggest that you can come across many problems trying to make the automation work completely without any additional input? I want the resulting letter to print off automatically and I do not need MSWord to be visible. The code I have managed to put together so far is below. How do I refer to a document set up with the merge fields and get the query results to go in there?
Any Help would be greatlly appreciated.
Private Sub runword_Click()
On Error GoTo Err_runword_Click
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
Set oMainDoc = oApp.Documents.Add
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetter
sDBPath = "C:\Documents and Settings\Carl\Desktop\Database Research\CarlClientdbAppt\Copy of ClientdbAppt.mdb"
.OpenDataSource Name:=sDBPath, _
Connection:="QUERY queryapptletter", _
SQLStatement:="SELECT * FROM [queryapptletter]"
Exit_runword_Click:
Exit Sub
Err_runword_Click:
MsgBox Err.Description
Resume Exit_runword_Click
End With
End Sub
Thanks in Advance.
Hi all.
I was wondering if anyone can help?
I have been trying to automate a mailmerge from Access 2002 to Word 2002 (xp) but with no luck. I have a query that I wish to use as the datasource. I have found some code for the 97 version but can't get it to work in xp. I have tried the knowledge-base and have gone through this forum but am still struggling. I want to use a button to launch the mailmerge.
Does anyone have an example? Threads I have read seem to suggest that you can come across many problems trying to make the automation work completely without any additional input? I want the resulting letter to print off automatically and I do not need MSWord to be visible. The code I have managed to put together so far is below. How do I refer to a document set up with the merge fields and get the query results to go in there?
Any Help would be greatlly appreciated.
Private Sub runword_Click()
On Error GoTo Err_runword_Click
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
Set oMainDoc = oApp.Documents.Add
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetter
sDBPath = "C:\Documents and Settings\Carl\Desktop\Database Research\CarlClientdbAppt\Copy of ClientdbAppt.mdb"
.OpenDataSource Name:=sDBPath, _
Connection:="QUERY queryapptletter", _
SQLStatement:="SELECT * FROM [queryapptletter]"
Exit_runword_Click:
Exit Sub
Err_runword_Click:
MsgBox Err.Description
Resume Exit_runword_Click
End With
End Sub
Thanks in Advance.
