Solved

access 2010 and word 2010 mailmerge using vba

Posted on 2015-02-17
6
329 Views
Last Modified: 2016-02-10
I am trying to initiate a Word 2010 mailmerge from Access 2010 that would:
- Run query in Access that run a make table
- Use a specific Word template
- Execute mailmerge that opens document based on template and links to my table
- Keep only the resulting mailmerge document open and close other docs based on the template

When I run the code below, the query seems to run and Word opens, but Word runs very slow -- almost as if there is some merging or activity taking place in the background. This happens even if I am merging only one record. It seems almost as if the code is not releasing the word document so I can click on the File menu to save, etc. It's literally stuck and the only way for me to exit it to click the close "X" at the top right corner of the Word doc to close the document.

What am I doing wrong?

Thanks for any insights. Code used is below.

Private Sub cmdMergeIT_Click()

DoCmd.SetWarnings False

DoCmd.OpenQuery "Art Docket Recommendations Word", acViewNormal

DoCmd.SetWarnings True

   'For use as TEMPLATE
        Dim objApp As Word.Application
   'For use as TEMPLATE
        Dim objWord As Word.Document
        '
        Dim ObjWord2 As Word.Document
        '
   'For use as TEMPLATE
        Set objApp = New Word.Application
   'For use as TEMPLATE on GRD Server - added from EE
        Set objWord = objApp.Documents.Add("K:\DUG\Templates\docketcharttemplate_newlayout_portrait.dot")
   ' Make Word visible.
        objWord.Application.Visible = True
   
   ' Set the mail merge data source as the DUG Access Database

   objWord.MailMerge.OpenDataSource _
     Name:="C:\Users\HT\My Documents\Gifts\DUG.mde", _
     LinkToSource:=True, _
       Connection:="TABLE tblDocketRecommendations", _
       SQLStatement:="SELECT * FROM [tblDocketRecommendations]"

   'Execute the mail merge.
   objWord.MailMerge.Execute
'=========================================================
' code  to close mail merge doc based on template
  For Each ObjWord2 In objApp.Documents
    If ObjWord2.Name = "Document1" Then
      ObjWord2.Close False
    End If
  Next

End Sub

Open in new window

0
Comment
Question by:htamraz1
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40614454
The most obvious thing is that you seem to be leaving the Word application that you have just created. There is a risk that you will have multiple Word applications open. This can create conflicts in Word. The modification below uses any existing instance in preference to creating a new one.
'...
   'For use as TEMPLATE
        On Error Resume Next
            Set objApp = GetObject(, "Word.Application")
        On Error GoTo 0
        If objApp Is Nothing Then
            Set objApp = CreateObject("Word.Application")
        End If
   'For use as TEMPLATE on GRD Server - added from EE
'...

Open in new window

0
 

Author Comment

by:htamraz1
ID: 40614539
Thanks GrahamSkan

Would you please include your code snippet with my previously sent code and resend the entire update (mine plus yours)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40614639
I tried to indicate the position of the code by bracketing it with your comments, but here it is in place.
Private Sub cmdMergeIT_Click()

DoCmd.SetWarnings False

DoCmd.OpenQuery "Art Docket Recommendations Word", acViewNormal

DoCmd.SetWarnings True

   'For use as TEMPLATE
        Dim objApp As Word.Application
   'For use as TEMPLATE
        Dim objWord As Word.Document
        '
        Dim ObjWord2 As Word.Document
        '
   'For use as TEMPLATE
        On Error Resume Next
            Set objApp = GetObject(, "Word.Application")
        On Error GoTo 0
        If objApp Is Nothing Then
            Set objApp = CreateObject("Word.Application")
        End If
   'For use as TEMPLATE on GRD Server - added from EE
        Set objWord = objApp.Documents.Add("K:\DUG\Templates\docketcharttemplate_newlayout_portrait.dot")
   ' Make Word visible.
        objWord.Application.Visible = True
   
   ' Set the mail merge data source as the DUG Access Database

   objWord.MailMerge.OpenDataSource _
     Name:="C:\Users\HT\My Documents\Gifts\DUG.mde", _
     LinkToSource:=True, _
       Connection:="TABLE tblDocketRecommendations", _
       SQLStatement:="SELECT * FROM [tblDocketRecommendations]"

   'Execute the mail merge.
   objWord.MailMerge.Execute
'=========================================================
' code  to close mail merge doc based on template
  For Each ObjWord2 In objApp.Documents
    If ObjWord2.Name = "Document1" Then
      ObjWord2.Close False
    End If
  Next

End Sub

Open in new window

The Mail Merge main document is not usually a template, because merge execution doesn't modify the document so it can simply be closed.
You can use a select query as an input, so if you create a Select version of your query, you can set it as the datasource at the document  design time. That way you can get away with little or no code
Private Sub cmdMergeIT2_Click()

   'For use as TEMPLATE
        Dim objApp As Word.Application
   'For use as TEMPLATE
        Dim objWord As Word.Document
        '
   'For use as TEMPLATE
        On Error Resume Next
            Set objApp = GetObject(, "Word.Application")
        On Error GoTo 0
        If objApp Is Nothing Then
            Set objApp = CreateObject("Word.Application")
        End If
   'Open the main document
        Set objWord = objApp.Documents.Open("K:\DUG\Templates\docketcharttemplate_newlayout_portrait.doc")
   ' Make Word visible.
    objWord.Application.Visible = True
   'execute h
    objWord.MailMerge.Execute
'=========================================================
' code  to close mail merge main doc
    objWord.Close False

End Sub

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:htamraz1
ID: 40615029
Thank you. This is helpful, but did not quite resolve the issue. I have gotten a little closer though. Perhaps you shed some light here.

I notice a strange and unexplained behavior that when Word 2010 opens in MAXIMIZED screen mode, I cannot click on any of the top menus (e.g. File, Home, Insert, etc.). However, when I restore the screen size, I can access the menus. This behavior is bizarre and unexplained. Have you ever heard of this?

It would be a plus to make sure the Word instance that opens is not in MAXIMIZED mode. If we can resolve this, I would be happy to put this post to rest.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40615067
I don't immediately know why your Word opens with what sounds like full-screen mode that is available in some other applications. In those, the {Esc} key usually puts it back to 'normal'
However this code:
objApp.WindowState = wdWindowStateNormal

Open in new window

0
 

Author Comment

by:htamraz1
ID: 40617307
In my case objApp.WindowState = wdWindowStateMinimize did the trick. The issue no longer happens after I added this to the code.

Thanks for your help and pointing me in the right direction.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

746 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

13 Experts available now in Live!

Get 1:1 Help Now