VB - user-defined type not defined
Nedenstående kode har jeg forsøgt at køre, men den melder fejl på linien: Dim WithEvents oApp As Word.ApplicationUnder referencer har jeg enabled Office 14.0 Object Library - bruger Win 7 - Office 2010.
Nogen, der kan hjælpe?
--- kode start ---
Dim WithEvents oApp As Word.Application
Private Sub Form_Load()
'Start Word.
Set oApp = CreateObject("Word.Application")
End Sub
Private Sub Command1_Click()
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
'Start a new main document for the mail merge.
Set oMainDoc = oApp.Documents.Add
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
'Set up the mail merge data source to Northwind.mdb.
sDBPath = "C:\Program Files\Microsoft Office\" & _
"Office14\SAMPLES\Northwind.mdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [Customers]"
'Add the field codes to the document to create the form letter.
With .Fields
Set oSel = oApp.Selection
.Add oSel.Range, "CompanyName"
oSel.TypeParagraph
.Add oSel.Range, "Address"
oSel.TypeParagraph
.Add oSel.Range, "City"
oSel.TypeText ", "
.Add oSel.Range, "Country"
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText "Dear "
.Add oSel.Range, "ContactName"
oSel.TypeText ", "
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText " This letter is to inform you..."
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText "Sincerely, [Your Name Here]"
End With
End With
'Perform the mail merge to a new document.
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute Pause:=False
End With
End Sub
Private Sub oApp_MailMergeAfterMerge(ByVal Doc As Word.Document, ByVal DocResult As Word.Document)
'When the mail merge is complete, 1) make Word visible,
'2) close the mail merge document leaving only the resulting document
'open and 3) display a message.
Doc.Close False
oApp.Visible = True
MsgBox "Mail Merge Complete: " & oApp.ActiveDocument.Name
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set oApp = Nothing
End Sub
--- kode stop ---