Word Report

brsawvel

Registered User.
Local time
Today, 15:08
Joined
Sep 19, 2007
Messages
256
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):

Code:
[/FONT][/COLOR]
[COLOR=black]Private Sub cmdPrint_Click()[/COLOR][COLOR=black][FONT=Courier New]
'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[/FONT][/COLOR][COLOR=black][FONT=Verdana][/FONT][/COLOR]
[COLOR=black].FormFields("fldRecipientAddress1").Result = Me!fldRecipientAddress1[/COLOR][COLOR=black][FONT=Courier New]
.FormFields("fldRecipientAddress2").Result = Me!fldRecipientAddress2[/FONT][/COLOR][COLOR=black][FONT=Verdana][/FONT][/COLOR]
[COLOR=black].FormFields("fldRecipientCityStateZip").Result = Me!fldRecipientCityStateZip[/COLOR][COLOR=black][FONT=Courier New]
.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[/FONT][/COLOR][COLOR=black][FONT=Verdana][/FONT][/COLOR]
[COLOR=black]


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?
 
For Dim appWord As Word.Application to work you need to select the object model for Word. In the VB editor you need to open the libraries.

Alt + F11
Select tools - references
Make the check box checked beside the object library for Microsoft Word
click OK

Do not go select happy and check all the boxes because these models add weight to the program running and you could end up with your app slowing to snail speed.
 
Thanks for the help on that....

However, For some reason, it doesn't bring up the Word Document unless I click the button twice (the code is in the "On Click" portion).

Also, the fields aren't populating with the data.

Any idea why?
 

Users who are viewing this thread

Back
Top Bottom