Dragunsfyre
Technical User
My Access 2K to Word 2K mail merge module is:
***********************************************************
Function MergeDoc(varDocName, varQryName)
Dim objWord As Word.Document
Set objWord = GetObject(varDocName, "Word.Document")
' Make Word visible
objWord.Application.Visible = True
' Set the mail merge data source as the Client database.
objWord.MailMerge.OpenDataSource _
Name:="C:\My Files\Databases\Client.mdb", _
LinkToSource:=True, _
Connection:="QUERY varQryName"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
' Now Print it
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
' Exit the Document without saving it and Exit Word
objWord.Application.ActiveDocument.Close SaveChanges = False
objWord.Application.Quit
End Function
***********************************************************
The code for the command button calling the function is:
***********************************************************
Private Sub btnPrintContract_Click()
On Error GoTo Err_PrintContract_Click
Dim varDocName As String
Dim varQryName As String
varDocName = "C:\My Files\Databases\Contract.doc"
varQryName = "qryContractData"
Call MergeDoc(varDocName, varQryName)
Exit_PrintContract_Click:
MsgBox "DONE"
Exit Sub
Err_PrintContract_Click:
MsgBox "Error #" & Err.Number & vbCr & Err.Description
Resume Exit_PrintContract_Click
End Sub
***********************************************************
The query qryContractData is:
***********************************************************
SELECT DISTINCTROW tblClientData.FileNumber, tblClientData.ClientFullName, tblClientData.SpouseFullName, tblClientData.ClientRole
FROM tblClientData
WHERE (((tblClientData.FileNumber)=[Me]![FileNumber]));
***********************************************************
FileNumber is the key field of the active record on the Form with the command button; the WHERE is my attempt to limit the query results to only that record. It is my intent to print one document tied to one record. The contract is too lengthy to code into an Access Report and is used for other purposes than as an Access Merge Document.
Contract.doc was created using Word Mail Merge, but after creation, was copied and pasted to a new document so that it would not be bound to a specific database.
When I click on the command button on the form, I'm getting an error message "Error #5922, Word was unable to Open the Data Source". When the execution halts, I have an instance of Word 2K with the file "Contract.Doc" active on screen, but since the data source won't open, it's still in the un-merged format. At least it's executing to that point. I can't seem to find my coding error.
Is the problem in the way I'm passing the query to the function or do I need additional code to execute the query before reaching the
objWord.MailMerge.OpenDataSource
code?
Any help is appreciated.
***********************************************************
Function MergeDoc(varDocName, varQryName)
Dim objWord As Word.Document
Set objWord = GetObject(varDocName, "Word.Document")
' Make Word visible
objWord.Application.Visible = True
' Set the mail merge data source as the Client database.
objWord.MailMerge.OpenDataSource _
Name:="C:\My Files\Databases\Client.mdb", _
LinkToSource:=True, _
Connection:="QUERY varQryName"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
' Now Print it
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
' Exit the Document without saving it and Exit Word
objWord.Application.ActiveDocument.Close SaveChanges = False
objWord.Application.Quit
End Function
***********************************************************
The code for the command button calling the function is:
***********************************************************
Private Sub btnPrintContract_Click()
On Error GoTo Err_PrintContract_Click
Dim varDocName As String
Dim varQryName As String
varDocName = "C:\My Files\Databases\Contract.doc"
varQryName = "qryContractData"
Call MergeDoc(varDocName, varQryName)
Exit_PrintContract_Click:
MsgBox "DONE"
Exit Sub
Err_PrintContract_Click:
MsgBox "Error #" & Err.Number & vbCr & Err.Description
Resume Exit_PrintContract_Click
End Sub
***********************************************************
The query qryContractData is:
***********************************************************
SELECT DISTINCTROW tblClientData.FileNumber, tblClientData.ClientFullName, tblClientData.SpouseFullName, tblClientData.ClientRole
FROM tblClientData
WHERE (((tblClientData.FileNumber)=[Me]![FileNumber]));
***********************************************************
FileNumber is the key field of the active record on the Form with the command button; the WHERE is my attempt to limit the query results to only that record. It is my intent to print one document tied to one record. The contract is too lengthy to code into an Access Report and is used for other purposes than as an Access Merge Document.
Contract.doc was created using Word Mail Merge, but after creation, was copied and pasted to a new document so that it would not be bound to a specific database.
When I click on the command button on the form, I'm getting an error message "Error #5922, Word was unable to Open the Data Source". When the execution halts, I have an instance of Word 2K with the file "Contract.Doc" active on screen, but since the data source won't open, it's still in the un-merged format. At least it's executing to that point. I can't seem to find my coding error.
Is the problem in the way I'm passing the query to the function or do I need additional code to execute the query before reaching the
objWord.MailMerge.OpenDataSource
code?
Any help is appreciated.