467,077 Members | 952 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

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

100+
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:

Expand|Select|Wrap|Line Numbers
  1. Public Sub procOpenMergedDoc(strFilePath As String)
  2. On Error GoTo ErrorHandler
  3.  
  4.     Dim strDbFilePath As String
  5.     Dim objWord As New Word.Application
  6.     Dim objDoc As Word.Document
  7.  
  8.     strDbFilePath = DLookup("DatabaseFilePath", "t_Settings")
  9.  
  10.     Set objDoc = objWord.Documents.Open(strFilePath)
  11.  
  12.     objDoc.MailMerge.OpenDataSource Name:=strDbFilePath, LinkToSource:=True, AddToRecentFiles:=False, _
  13.     Connection:="q_Template", SQLStatement:="SELECT * FROM [q_Template]"
  14.     objWord.ActiveDocument.MailMerge.Execute
  15.     objWord.Application.Visible = True
  16.  
  17. Exit_Error:
  18.     Set objWord = Nothing
  19.     Set objDoc = Nothing
  20.     Exit Sub
  21. ErrorHandler:
  22.     MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
  23.     objWord.Quit
  24.     Resume Exit_Error
  25. End Sub
  26.  
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!
Mar 3 '11 #1

✓ answered by TheSmileyCoder

The word application also has the same method so could try:
Expand|Select|Wrap|Line Numbers
  1. objWord.ActiveWindow.WindowState = wdWindowStateNormal
I can't recreate your exact "conditions" since I have no experience in doing word document merging.

  • viewed: 2318
Share:
5 Replies
TheSmileyCoder
Expert Mod 2GB
I dont know for sure since I haven't worked much in word automation, but have you tried objWord.SetFocus or objDoc.SetFocus?
Mar 3 '11 #2
100+
SetFocus does not appear to be an attribute of either objWord or objDoc.
Mar 4 '11 #3
TheSmileyCoder
Expert Mod 2GB
Like I said, no Word automation expert :P

I tried this:
Expand|Select|Wrap|Line Numbers
  1. 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.
wdWindowStateNormal works also.
Mar 4 '11 #4
100+
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:

Expand|Select|Wrap|Line Numbers
  1.     objWord.Visible = True
  2.     objWord.Documents(2).Close wdDoNotSaveChanges
  3.     objDoc.ActiveWindow.WindowState = wdWindowStateMaximize
  4.  
Mar 4 '11 #5
TheSmileyCoder
Expert Mod 2GB
The word application also has the same method so could try:
Expand|Select|Wrap|Line Numbers
  1. objWord.ActiveWindow.WindowState = wdWindowStateNormal
I can't recreate your exact "conditions" since I have no experience in doing word document merging.
Mar 6 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Aparna | last post: by
2 posts views Thread by Charles A. Lackman | last post: by
2 posts views Thread by Tim | last post: by
8 posts views Thread by gazza67 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.