I'm opening a merged word document from Access using VBA and I'm having two problems. When the user clicks the Merge Template button code executes that sets a query for the data merging and at the end of the code I call a UDF that opens the merged document. This is the code in the UDF:
This code works. It opens a word document with the merged data. However, there are two issues:
1) Besides opening the merged Word document it also opens the original Word template. In the one I have setup, I'm merging labels. So I created a label template and inserted my merge fields. However, when this code runs, it opens the merge template showing all the merge fields like <<MailingSal> >, <<AddressLine1> >, etc. along with a word document that has the address data in it. How can I not have the original merge template open but just a Word doc with the merged data?
2) When the Word doc opens, it doesn't open on the screen like when you launch an application. Instead, a Word icon appears in the taskbar and you have to click on it to bring up the doc. Is there a way I can get it to open so that it opens on the screen and the user doesn't think that nothing happened?
Thanks for your help!
Code:
Public Sub procOpenMergedDoc(strFilePath As String) On Error GoTo ErrorHandler Dim strDbFilePath As String Dim objWord As New Word.Application Dim objDoc As Word.Document strDbFilePath = DLookup("DatabaseFilePath", "t_Settings") Set objDoc = objWord.Documents.Open(strFilePath) objDoc.MailMerge.OpenDataSource Name:=strDbFilePath, LinkToSource:=True, AddToRecentFiles:=False, _ Connection:="q_Template", SQLStatement:="SELECT * FROM [q_Template]" objWord.ActiveDocument.MailMerge.Execute objWord.Application.Visible = True Exit_Error: Set objWord = Nothing Set objDoc = Nothing Exit Sub ErrorHandler: MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical objWord.Quit Resume Exit_Error End Sub
1) Besides opening the merged Word document it also opens the original Word template. In the one I have setup, I'm merging labels. So I created a label template and inserted my merge fields. However, when this code runs, it opens the merge template showing all the merge fields like <<MailingSal> >, <<AddressLine1> >, etc. along with a word document that has the address data in it. How can I not have the original merge template open but just a Word doc with the merged data?
2) When the Word doc opens, it doesn't open on the screen like when you launch an application. Instead, a Word icon appears in the taskbar and you have to click on it to bring up the doc. Is there a way I can get it to open so that it opens on the screen and the user doesn't think that nothing happened?
Thanks for your help!
Comment