Help with a VBA code (different one than previously)

dbaker

New member
Local time
Today, 00:55
Joined
Mar 8, 2011
Messages
5
Ok, I've changed directions somewhat...I've scrapped the code which I previously asked about and I'm starting over with a new code. Here is the rundown:

I'm very, very new to VBA and somewhat new to Access. I'm trying to fill in fields in WORD with data from ACCESS. Here is the current code I'm attemping to use:

Code:
Private Sub cmdCreatePO_Click()
'Print customer slip for current customer.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("C:\Documents and Settings\dgbaker\Desktop\vendor\po.doc", , True)
With doc
.FormFields("fldCONTACT").Result = Me!Contact
.FormFields("fldVENDOR").Result = Me!Vendor
.FormFields("fldADDRESS").Result = Me!Address
.FormFields("fldCITY").Result = Me!City
.FormFields("fldSTATE").Result = Me!State
.FormFields("fldZIP").Result = Me!Zip
.FormFields("fldPHONE").Result = Me!Phone
.FormFields("fldEMAIL").Result = Me!Email
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub

and here is the error message I am getting when I attempt to run it:
Compile error:
User-defined type not defined

Can someone please help me with fixing whatever is wrong here. Thanks in advance
 
hmm..not sure if that is correct or not. I did, however, get the code to work by referencing Microsoft Word Object Library. Thanks for the help
 
In your With.....End with statement. Instead of With doc try:

With appWord.ActiveDocument
(Rest of your statement)
End With
 

Users who are viewing this thread

Back
Top Bottom