PDA

View Full Version : Solved: InsertFile problem



jwise
06-05-2012, 11:35 AM
I have an Excel spreadsheet which uses Word to print. Each data row becomes a page of summary data; the Word file is a document template with several bookmarks for each Excel-supplied number. The original version of this code worked but it created a single page print file for each row. I'm trying to make a single Word document which would enable me to print it much nicer (other print jobs get in the middle of mine). Here is the code snippet in question:
Set wrdApp = CreateObject("Word.Application")
wrdApp.Documents.Add

For i = 3, lastRow
rngDoc.InsertFile Filename:="Recap.dot"
... 'Fill in bookmarks with Excel data
Next i

... 'other processing
I have tried this with Selection instead of Range and received a similar error. My guess is that it has something to do with my rngDoc variable, but on the first iteration, the document is blank and there is nothing to select or to place within a range.

Ideas?

I freely admit I usually write Excel macros and not Word.

This is Office 2003.

Frosty
06-05-2012, 11:42 AM
Where do you set rngDoc?

You may need to reset your range after each insert... something along the lines of...

For i = 3 to LastRow
Set rngDoc = wrdApp.ActiveDocument.Content
rngDoc.Collapse wdCollapseEnd
rngDoc.InsertFile FileName:="Recap.dot"
'do your bookmark processing
Next

jwise
06-05-2012, 12:44 PM
Frosty,

Thank you. This solved my problem. On to the next hurdle!