Learn how to a build a cloud-first strategyRegister Now


Opening merge document from access

Posted on 2014-08-05
Medium Priority
Last Modified: 2014-08-06

I've written the following code to open a word document.  It works but when opening a merge document, the merge is not executed.  However, when I open the document, it executes the merge.  I'm not sure what is wrong. Thanks.

Public Sub PrintExternalWordDocs(strDocPath As String)
    ' Comments:
    ' Params  : strDocPath
    ' Modified:
    On Error GoTo PROC_ERR
    Dim intStrLength As Integer
    Dim strDocName As String
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    Dim wdSendtoNewDocument As String
    Dim objPrinter As Printer
    Set objPrinter = Application.Printer
    With objWord
        .Activeprinter = objPrinter.DeviceName
        .Visible = True
        .Documents.Open (strDocPath)
    End With
    Set objWord = Nothing
    Exit Sub
    MsgBox Err.Number & vbTab & Err.Description, vbCritical, "basPrintExternalDocuments.PrintExternalWordDocs"
    Resume PROC_EXIT
End Sub

Open in new window

Question by:chtullu135
  • 2
LVL 31

Accepted Solution

Helen Feddema earned 2000 total points
ID: 40241763
Here is some code to create a merge document from a template (and perform the merge):
   With doc
      .MailMerge.OpenDataSource Name:=strTextFile, _
      If Nz(InStr(strTemplateName, "Label")) > 0 Then
         .MailMerge.MainDocumentType = wdMailingLabels
         .MailMerge.MainDocumentType = wdFormLetters
      End If
      .MailMerge.Destination = wdSendToNewDocument
      'Set another Document variable to the newly merged document,
      'to ensure that the correct document is saved
      Set docMerge = appWord.ActiveDocument
      .Close savechanges:=wdDoNotSaveChanges
   End With
   docMerge.SaveAs2 strSaveNamePath

Open in new window

In this case, the merge document has a text file as the record source, filled from an Access query.  This is a segment of much longer code from my Working with Word ebook.

Author Comment

ID: 40241981
In my case, I am using an access query on an access database.  The merge document is a preexisting document.  I am just trying to open the merge document from Access and have it execute its merge.  Much like opening the document and having the merge execute.  The user will be printing the document when it opens and will then close it where it will be saved to the same location it was opened from

Author Closing Comment

ID: 40245241
Thanks again

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question