MailMerge with VBA

K

Karl H

I am trying to open a mail merge document to print a certificate using VBA. I
took a course where they used the code below to open a different document.

Using the following code, the debugger stops at:Set pobjMerge =
pobjWord.MailMerge

When the cursor is over the highlighted line, a tool tip says "pobjWord =
Nothing"

Can you tell me what I'm doing wrong?

Code:
Option Compare Database
Option Explicit

Public Function WriteToWord()
'Execute mailmerge so that a certificate
'is generated.
Dim pobjWord As Word.Document
Dim pobjMerge As Object
Dim pstrPath As String
pstrPath = "F:\aFMergeBld2.doc"
'Open the Word document. Word is open but not visible.
Set pobjWord = GetObject("F:\aFMergeBld2.doc")
'Set a pointer to mailmerge object that controls merges
Set pobjMerge = pobjMerge.MailMerge
'The destination for the merge is a new document
pobjMerge.Destination = wdSendToNewDocument
'Execute the merge and create a new document
pobjMerge.Execute
'Make MS Word appear in Windows
pobjWord.Application.Visible = True
'Close the aFAAMergeBld2 so the user does no see the
'merge fields. The new document remains open.
pobjWord.Close
'Remove the pointers to the MS Word objects.
Set pobjMerge = Nothing
Set pobjWord = Nothing
End Function

I have the "on click" property set = WriteToWord()

Thank you,
Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top