How to Correct Issues Opening a Merged Word Document in VBA?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

    How to Correct Issues Opening a Merged Word Document in VBA?

    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:

    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
    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!
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I dont know for sure since I haven't worked much in word automation, but have you tried objWord.SetFocu s or objDoc.SetFocus ?

    Comment

    • bullfrog83
      New Member
      • Apr 2010
      • 124

      #3
      SetFocus does not appear to be an attribute of either objWord or objDoc.

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        Like I said, no Word automation expert :P

        I tried this:
        Code:
        objDoc.ActiveWindow.WindowState = wdWindowStateMaximize
        which works if I run it as the result of a form/control event, but NOT if I run it from the immediate window.
        wdWindowStateNo rmal works also.

        Comment

        • bullfrog83
          New Member
          • Apr 2010
          • 124

          #5
          Me neither but I appreciate the assistance! When I append that line of code I get the error "The object invoked has disconnected from its client".

          I also figured out how to close the merge template so my above code is revised as thus:

          Code:
              objWord.Visible = True
              objWord.Documents(2).Close wdDoNotSaveChanges
              objDoc.ActiveWindow.WindowState = wdWindowStateMaximize

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            The word application also has the same method so could try:
            Code:
            objWord.ActiveWindow.WindowState = wdWindowStateNormal
            I can't recreate your exact "conditions " since I have no experience in doing word document merging.

            Comment

            Working...