hello,
I have a db whit a query, now when a user is clicking on a button in a form there most be print a number of letters whit some data of the query.
I have now the following code:
Code:
Public Sub MergeToWord(strDocName As String, MyQuery As String)
Dim objApp As Object
'DoCmd.Close
'Change cursor to hourglass
DoCmd.Hourglass True
'Open Mailmerge Document
'Start Word
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True 'Make it visible
.Documents.Open strDocName 'Open the Mailmerge Document
'Use the Query defined in the arguments as the datasource
.ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, Connection:="QUERY " & MyQuery
End With
'print and close Document
With objApp
.ActiveDocument.MailMerge.Execute Pause:=True
.ActiveDocument.PrintOut Background:=False
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 'Avoid Saving over your template
.Quit SaveChanges:=wdDoNotSaveChanges 'close all documents
End With
Set objApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 4157
End Select
DoCmd.Hourglass False 'Cursor back to normal
the word file has all the correct merg reference and no connection.
only when i am running the code it will stop in word and ask me to select the correct table/query. How ever i give the query in the line: Connection:="QUERY " & MyQuery
I have try moste options under ActiveDocument.MailMerge.OpenDataSource but don't get it to work. Does someone now how to get round this message form word?
Richard Karelse