Run Mail Merge from VBA

jdc

New member
Local time
Today, 09:49
Joined
Feb 27, 2013
Messages
3
I created a Command Button in a Form of Access 2007 to run a Mail Merge. I used VBA script that provided by ajetrumpet. It worked in his sample database, but it didn't work when I tried in my database. I got the following error message:

"User-defined type not defined"
(Dim WithEvents oApp As Word.Application)

Could anybody help me to resolve this problem?

Thanks.



Option Compare Database
Dim WithEvents oApp As Word.Application

Private Sub Command0_Click()

Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String

Set oMainDoc = oApp.Documents.Open("D:\DOCUMENT_MailMerge")
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "D:\DATABASE_MailMerge.accdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblDrAbstract]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visible = True
oApp.Application.WindowState = 1
oApp.ActiveWindow.WindowState = 1
End Sub

Private Sub Form_Load()
Set oApp = CreateObject("Word.Application")
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set oApp = Nothing
End Sub
 
Last edited:
That uses early binding. You'd need to check the reference to Word in Tools/References.
 
Thanks Paul. I added "Microsoft Word 12.0 Object Library" to References. It resolved the error message "User-defined type not defined". Now the Tools/References has the following References:

Visual Basic for Applications
Microsoft Access 12.0 Object Library
OLE Automation
Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Word 12.0 Object Library

But the program stops running at"
Set oMainDoc = oApp.Documents.Open("d:\DOCUMENT_MailMerge")

I get the following Error message:
Run-time error '91' object variable or with block variable not set

Could you please help me to resolve this Run-time error '91'?

I appreciate your help very much.

Thanks.
jdc
 
That implies you haven't set the oApp variable. I'm not sure what that line earlier does.
 
Thanks Paul. I added "Microsoft Word 12.0 Object Library" to References. It resolved the error message "User-defined type not defined". Now the Tools/References has the following References:

Visual Basic for Applications
Microsoft Access 12.0 Object Library
OLE Automation
Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Word 12.0 Object Library

But the program stops running at"
Set oMainDoc = oApp.Documents.Open("d:\DOCUMENT_MailMerge")

I get the following Error message:
Run-time error '91' object variable or with block variable not set

Could you please help me to resolve this Run-time error '91'?

I appreciate your help very much.

Thanks.
jdc

Try declaring the oApp object simply as
Code:
Dim oApp as Word.Application

Best,
Jiri
 
That's half the battle. ;)
 

Users who are viewing this thread

Back
Top Bottom