Automation with Word

sabll

New member
Local time
Today, 09:43
Joined
Apr 15, 2008
Messages
2
Hi, basically I have a button which creates a worded document, giving full details of a customer and an order they have placed. However, the customer details are in one table and the order details in another. Below is my code I have used, everything seems to run ok except the customer details don't seem to appear in the document just the order details. Thanks for any help in advance.


Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As ADODB.Recordset
'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
'Populate recordset object.
Set rst = New ADODB.Recordset

rst.Open Me.RecordSource, CurrentProject.Connection

'Cycle through records to fill Word form fields.
Do While Not rst.EOF
Set doc = appWord.Documents.Open("C:\Users\Matt\Desktop\Uni\BSADC\Order.dotx", , True)

With doc

.FormFields("CustomerCode").Result = rst!CustomerCodeNo
.FormFields("CustomerName").Result = rst!CustomerName
.FormFields("CustomerAddress").Result = rst!CustomerAddress
.FormFields("CustomerPostcode").Result = rst!CustomerPostcode
.FormFields("CustomerTelephone").Result = rst!CustomerTelNo
.FormFields("Order").Result = rst!DeliveryNo

.Visible = True
.Activate
rst.MoveNext
End With
Loop
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
 

Users who are viewing this thread

Back
Top Bottom