MS Word Automation - .Net 2.0

D

Derek Hart

I have a vb.net winforms application that loops through several thousand
word documents and prints them, with the filenames being read from a SQL
database. I am using late bound code to load MS Word. I have tried loading
Word for each document, printing the doc, and exiting Word. I have also
tried loading Word for 100 documents at a time, exiting Word, and reloading
Word for the next 100 documents. I have tried many scenarios. Around
approximately 1500 documents Word seems to stop, and I will get an error
""exception has been thrown by the target of an invocation" - and Windows XP
itself is mostly frozen (cannot load any apps), until I exit my winforms
app. I am about to move to loading code inside an MS Word template directly
instead of trying to automate from dot net. I am not sure if somebody will
have direct advice on how to get this working in my winforms app, but it
would be helpful to know if it is suggested to not try to automate Word like
this from a vb.net app with such heavy printing in Word. Or should I use
VB6 to automate it. I have to use late bound because I may print in
different versions of Word. Any thoughts?

Derek Hart
 
N

Norman Yuan

Have you tried to create a SINGLE Word object, and use
ObjWordApp.Documents.Open()/Close() to open/close a document in each loop.
This way, you will not create Word object 1500 time (only once), you will
not open 100 document at a time (you only open one at a time).

Something like (psedocode):

Dim objWordApp as Object
Dim objDoc As Object

objWordApp=CreateObject("Word.Application)

For Each docFileName In DocFileNameList

'Open each document, one at a time
objDoc=objWordApp.Documents.Open(docFileName)

'Do your printing/processing with the Document object

objDoc.Close(False)

Next

objWordApp.Quit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top