Hello,
I'm trying to get form fields in a Word document to fill automatically, via macro, from an Excel worksheet.
I've created a custom icon on the standard toolbar for users to click, which will print invoices for each row with data in the worksheet. Essentially the macro is to be mostly automated.
I have the following code...
Code
Dim appExcel as Excel.Application
Set appExcel = CreateObject("Excel.Application")
Dim OrderRow as Variant
Dim OrdersFile as Variant
OrdersFile = appExcel.GetOpenFilename("Excel files (*.xls), *.xls")
appExcel.Workbooks.Open OrdersFile, , True
OrderRow = appExcel.Worksheets("Orders").Range("A2")
Do Until IsEmpty(OrderRow)
With ActiveDocument
.FormFields("StockNum").Result = OrderRow
.FormFields("Description").Result = OrderRow.Offset(1, 0)
.FormFields("UnitPrice").Result = OrderRow.Offset(2, 0)
.FormFields("SubTotal").Result = OrderRow.Offset(3, 0)
.FormFields("Insurance").Result = OrderRow.Offset(4, 0)
.FormFields("Gift").Result = OrderRow.Offset(5, 0)
.FormFields("Total").Result = OrderRow.Offset(6, 0)
.PrintOut
End With
OrderRow = OrderRow.Offset(0, 1)
Loop
ActiveDocument.Close wdDoNotSaveChanges
appExcel.Quit
Set appExcel = Nothing
Display More
When I run the process, it stops on the second line within the With statement and says "Object Required". I'm puzzled because if I go back and look at the document the correct Stock Number is filled in from the worksheet data.