brsawvel
10-06-2009, 03:26 AM
Hello,
I have a two part question..
I am trying to create reports using the Word Application vs. using Access reports. I grabbed some code off the internet and placed it on the "on click" command of a button, but received a "user not defined" error pointing to the "Dim appWord As Word.Application" portion of the code. Here is the code (with my fields):
Private Sub cmdPrint_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:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldRecipientOrganization").Result = Me!fldRecipientOrganization
.FormFields("fldRecipientDivision").Result = Me!fldRecipientDivision
.FormFields("fldRecipientAddress1").Result = Me!fldRecipientAddress1
.FormFields("fldRecipientAddress2").Result = Me!fldRecipientAddress2
.FormFields("fldRecipientCityStateZip").Result = Me!fldRecipientCityStateZip
.FormFields("fldSubject").Result = Me!fldSubject
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
Is there something wrong with this code?
Also, on the word document I've created, I am not sure if I have the fields linked right (don't know cause I haven't gotten the code above to work first). I selected INSERT>>FIELD>>FORMULA and typed in the names of the fields (i.e. =fldSubject). Is that right?
I have a two part question..
I am trying to create reports using the Word Application vs. using Access reports. I grabbed some code off the internet and placed it on the "on click" command of a button, but received a "user not defined" error pointing to the "Dim appWord As Word.Application" portion of the code. Here is the code (with my fields):
Private Sub cmdPrint_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:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldRecipientOrganization").Result = Me!fldRecipientOrganization
.FormFields("fldRecipientDivision").Result = Me!fldRecipientDivision
.FormFields("fldRecipientAddress1").Result = Me!fldRecipientAddress1
.FormFields("fldRecipientAddress2").Result = Me!fldRecipientAddress2
.FormFields("fldRecipientCityStateZip").Result = Me!fldRecipientCityStateZip
.FormFields("fldSubject").Result = Me!fldSubject
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
Is there something wrong with this code?
Also, on the word document I've created, I am not sure if I have the fields linked right (don't know cause I haven't gotten the code above to work first). I selected INSERT>>FIELD>>FORMULA and typed in the names of the fields (i.e. =fldSubject). Is that right?