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
"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: