Solved

access 2010 and word 2010 mailmerge using vba

Posted on 2015-02-17
6
344 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

830 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