Opening merge document from access

Posted on 2014-08-05
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
    LVL 31

    Accepted Solution

    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

    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

    Thanks again

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now