473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

124 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.

5 2525
TheSmileyCoder
2,322 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
bullfrog83
124 100+
SetFocus does not appear to be an attribute of either objWord or objDoc.
Mar 4 '11 #3
TheSmileyCoder
2,322 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
bullfrog83
124 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
2,322 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

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

Similar topics

3
by: Aparna | last post by:
Is there any way to open an MS-Word document from JSP Page or Java Coding? Please suggest.
0
by: Sivaraman.S | last post by:
Issue: When I am opening a word document or a template from an ASP.NET Web page (language used VB.NET Visual Studio Version 2003) using Word.ApplicationClass object (Reference Word 11.0 object...
2
by: Charles A. Lackman | last post by:
Hello, I have an application that opens a word document like so: With WrdApp ..Documents.Open(System.Windows.Forms.Application.StartupPath + "\Template.doc", missing, missing, missing,...
2
by: Tim | last post by:
I am trying to open a Word 97 document and I am getting a "QueryInterface for interface Word._Application failed" error message at the code line below. Any Suggestions? I have installed office XP...
8
by: gazza67 | last post by:
Hi, I want to do something that I thought would be simple but i cant seem to work it out, perhaps someone out there could help me. I want to browse for a file (it will be a word document),...
4
by: sunil | last post by:
Hi, I have to open a word document from my aspx page.The Page_Load method contains the following code System.IO.TextWriter tx = Response.Output; System.IO.StreamReader sr = new...
0
by: =?Utf-8?B?UEs5?= | last post by:
I am trying to open a hyperlink that points to a Microsoft Word document in my ASP.NET 2.0 application. The error message is: Path 'OPTIONS' is forbidden I have researched this error but have...
0
by: Andy Fish | last post by:
Hi, I have a web application and I would like to serve the user a word document and have word open it up using webdav. I have read kb 838028 about office protocol discovery, but this only...
1
by: vin4tony | last post by:
Anyone plz help to open a word document using asp.net.I used a code ..but it shown access denied...Can anyone help me.. my error is Access is denied. Description: An unhandled exception occurred...
1
by: captainwin | last post by:
Here's the problem: - Website is ASP.NET 1.0 - Server is Windows Server 2003 - Browser is IE 6/7 - Client requests a mail merged MS Word 97 - 2003 document on the website, and the document gets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.