Word Report

brsawvel

Registered User.
Local time
Yesterday, 22:09
Joined
Sep 19, 2007
Messages
256
I am trying to create reports using the Word Application vs. using Access reports.


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?


Code:
[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]
[COLOR=black][FONT=Courier New]Private Sub cmdPrint_Click()[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]'Print customer slip for current customer.[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Dim appWord As Word.Application[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Dim doc As Word.Document[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]'Avoid error 429, when Word isn't open.[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]On Error Resume Next[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Err.Clear[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]'Set appWord object variable to running instance of Word.[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Set appWord = GetObject(, "Word.Application")[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]If Err.Number <> 0 Then[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]'If Word isn't open, create a new instance of Word.[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Set appWord = New Word.Application[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]End If[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Set doc = appWord.Documents.Open("C:\WordForms\CustomerSlip.doc", , True)[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]With doc[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldRecipientOrganization").Result = Me!fldRecipientOrganization[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldRecipientDivision").Result = Me!fldRecipientDivision[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldRecipientAddress1").Result = Me!fldRecipientAddress1[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldRecipientAddress2").Result = Me!fldRecipientAddress2[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldRecipientCityStateZip").Result = Me!fldRecipientCityStateZip[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].FormFields("fldSubject").Result = Me!fldSubject[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].Visible = True[/FONT][/COLOR]
[COLOR=black][FONT=Courier New].Activate[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]End With[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Set doc = Nothing[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Set appWord = Nothing[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]Exit Sub[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]errHandler:[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]MsgBox Err.Number & ": " & Err.Description[/FONT][/COLOR]

[COLOR=black][SIZE=3][FONT=Times New Roman]End Sub[/FONT][/SIZE][/COLOR]
[COLOR=black][FONT=Times New Roman][SIZE=3]
[/COLOR]
 
Here is my best advice for using Access and Word together.

DON'T

I have been using Access for many years and I have never found a way to make it work properly.
I always:
-create a query in Access which contains the data I want in my Word report
-use the mail merge fucntion in Word to get the data I need
-run the whole thing from Word.

Yes, there is a method to do all this in Access but despite dozens of attempts, I can't make it work. There are always non justified lines and out of alignment tables in the Access version. The Word mail merge makes it perfect as it has text wrap and the like.
 

Users who are viewing this thread

Back
Top Bottom