VBA Doesn't Open Mail Merge Word Document.

eckert1961

Registered User.
Local time
Yesterday, 19:38
Joined
Oct 25, 2004
Messages
90
SOLVED:VBA Doesn't Open Mail Merge Word Document.

Hello,

I have an email mail merge document, "Email Receipt.doc" that I created in Winword 2003. In my Access 2003 database I want to open this document from a command button on my main form. I added the following code.

Code:
Dim objWord As Word.Document
    Set objWord = GetObject("K:\My Documents\Winword\Email Receipt.doc", "Word.document")
    ' Make Word visible
    objWord.Application.Visible = True

When I run the code, Word does launch but the document doesn't open and no error is reported. If I select the document from Word it opens up fine.

I've tried this code with regular documents and they open without a problem.
Is there any additional code required to make this work with a mail merge document?

The data source is a table, "tblEmailReceipts" that get's populated by a Make-Table query. The following is the sql statement from this query.

Code:
SELECT Payments.PaymentID, Payments.MemberID, Payments.TrainingDues, Payments.JKFDues, Payments.Other, [TrainingDues]+Payments.JKFDues+[Other] AS [Total Payment], ConvertCurrencyToEnglish([Total Payment]) AS NumbertoEnglish, Payments.CheckNumber, Payments.PaymentDate, Payments.PaymentMethodID, Payments.Description, Payments.ReceiptPrinted, Members.FirstName, Members.LastName, Members.ParentsNames, Members.Email INTO tblEmailReceipts
FROM Members LEFT JOIN Payments ON Members.MemberID = Payments.MemberID
WHERE (((Payments.ReceiptPrinted)=No));

Any assistance would be greatly appreciated.

Regards,
Chris
 
Last edited:
It turns out that I was wrong when I stated that when I run the code Word opens but not the document, "Email Receipt.doc". Nothing actually happens, including no error reported when I run the code.

If I change the filename to any regular document then everything runs without a problem.

Any ideas of what I need to change to make this work? Thanks.

Chris
 
Well this is embarrasing. It turns out that my path was incorrect. I changed the path to "K:\My Documents\Email Receipt.doc". Now when I run the code the mail merge document opens without error.

Regards,
Chris
 
I am trying to do this exact thing. I am new to Access and VBA. I am using your code, but with my own path and file name. When I click the command button in the form, I get the message from Microsoft Visual Basic: "Compile error: Invalid outside procedure." The word "Set" in my code is highlighted.

Any tips on resolving this problem? Thanks much.
 
Sounds like you have some code outside of a procedure (function or sub) and it needs to be inside it. Can you post the code you have?

Remember to use code tags, like this:

codetag001.png
 
Thanks, Bob. Like I said, I am new to VBA and Access. I am trying to fix something that never worked in an existing form. It is in a module with other subs. I kept the same style as the others. Here is what I have:
Code:
Private Sub OpenDoc_Click()
On Error GoTo Err_OpenDoc_Click
 
Dim objWord As Word.Document
Set objWord = GetObject("D:\MyPath\AnyWordDoc.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
 
Exit_OpenDoc_Click:
    Exit Sub
 
Err_OpenDoc_Click:
    MsgBox Err.Description
    Resume Exit_OpenDoc_Click
 
End Sub
The debugger highlights "objWord As Word.Document" and gives the message "User defined type not defined." I think that I might need an additional library to use Word.Document. I am using these libraries now:

Visual Basic For Applications
Microsoft Access 12.0 Object Library
OLE Automation
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Office 12.0 Access database engine Object Library

Thanks much.
 
Last edited by a moderator:
You would need a reference to

Microsoft Word 12.0 Object Library
 
And you didn't do like I asked in the screenshot, which is to add CODE tags to your code in your POST on the forum. I went in and modified your post to add them in so that the code would show up right. Please note that.
 
Sorry about the code tags. Microsoft Word 12.0 Object Library was exactly the right solution. Thanks!
 
Lots of ways to open a file without setting references. Easiest is...


Application.FollowHyperlink "D:\MyPath\AnyWordDoc.doc"
 

Users who are viewing this thread

Back
Top Bottom