By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Word Interop

P: n/a
I am using late bound Microsoft Word integration with a vb.net winforms
application. If I run code such as the following:

Dim objWord As Object
Dim objWrdDoc As Object
Dim count As Integer
Dim filename As String
filename = "c:\temp\mergedata\1.doc"
objWord = CreateObject("Word.Application")
For count = 1 To 10000
objWrdDoc = objWord.Documents.Open(filename)
objWrdDoc.PrintOut(BackGround:=False)
objWrdDoc.Close(wdDoNotSaveChanges)
Next
objWord.Quit(wdDoNotSaveChanges)

This will work with a huge loop, way more than even 10,000. Now in my actual
program I have this code, except much more logic is done to determine which
documents to print (the variable filename is constantly changing). The
objWord object is passed into another routine, and it is always passed
ByRef. All the other logic works fine, but after about 11000 documents,
Word crashes. It does not crash with a blue screen, but it just stops, and I
cannot click almost anything in Windows unless I exit my winforms
application, then I can use Windows normally. I have tried every variation
can find on how to use a late bound Word object properly, and clean it up
properly, such as statements such as these:

objWord = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
System.GC.Collect()
System.GC.WaitForPendingFinalizers()

I have tried DoEvents and other things. Are there big problems running a
Word Object on the UI Thread? I understand about all the issues of not
using Word on the server that are described in the Microsoft articles, but
this application will be attended. Word simply crashes when doing this
process.

So I am looking at possibly calling Word and running VBA code in a Word
template, which I know should be pretty reliable. Or should I use VB6 to do
this? Since I can get the above code working, perhaps I should write
another .Net winforms dll that can be called to do this, but should I call
that dll late bound, and if I call it late bound, would that put it on a
separate thread?

Please help and open up a dialog on this!!!

Thank You,
Derek


May 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I have a Windows Service that accepts requests to convert word documents
to .pdf (it opens the document, prints it via Adobe PDF printer and
closes the document). It does this several times a day and stays
running 24/7.

This service is fairly new and has been running for a few months so far
without any problems (although it has probably not processed more than 1
or 2 thousand documents in a row before a windows update or some other
server maintenance requires the server to restart, thus restarting my
service and recycling word). For what it's worth, here are snippets:

The Word object is defined at the class level of the conversion engine:

Private WithEvents objWord As Word.ApplicationClass

Starting up the word object:

Me.objWord = New Microsoft.Office.Interop.Word.ApplicationClass

'Turn off possible UI hangups...
With Me.objWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlert sNone
End With

Opening a document:

Dim objWordDocument As Word.Document
objWordDocument = Me.objWord.Documents.Open(<string>)

Printing a document:

objWordDocument.PrintOut(Background:=False, Copies:=<integer>,
PrintToFile:=<boolean>)

Closing the document:

objWordDocument.Close(Word.WdSaveOptions.wdDoNotSa veChanges)
objWordDocument = Nothing

Shutting down word:

'In case any documents are still open...
For Each objDocument As Word.Document In Me.objWord.Documents
objDocument.Close(Word.WdSaveOptions.wdDoNotSaveCh anges)
Next

Me.objWord.Quit(Word.WdSaveOptions.wdDoNotSaveChan ges)
Me.objWord = Nothing

To date this service has performed without error, but granted it has
never processed more than 10k documents in a row. Perhaps there is a
memory leak in Word and you should recycle your Word application object
every 5k - 10k documents or so, or perhaps there is something in the
above code that can assist you.

Derek Hart wrote:
I am using late bound Microsoft Word integration with a vb.net winforms
application. If I run code such as the following:

Dim objWord As Object
Dim objWrdDoc As Object
Dim count As Integer
Dim filename As String
filename = "c:\temp\mergedata\1.doc"
objWord = CreateObject("Word.Application")
For count = 1 To 10000
objWrdDoc = objWord.Documents.Open(filename)
objWrdDoc.PrintOut(BackGround:=False)
objWrdDoc.Close(wdDoNotSaveChanges)
Next
objWord.Quit(wdDoNotSaveChanges)

This will work with a huge loop, way more than even 10,000. Now in my actual
program I have this code, except much more logic is done to determine which
documents to print (the variable filename is constantly changing). The
objWord object is passed into another routine, and it is always passed
ByRef. All the other logic works fine, but after about 11000 documents,
Word crashes. It does not crash with a blue screen, but it just stops, and I
cannot click almost anything in Windows unless I exit my winforms
application, then I can use Windows normally. I have tried every variation
May 27 '07 #2

P: n/a
Just out of curiosity, what are you using to print to pdf? What driver or
software application?

Derek Hart

"asdf" <as**@asdf.comwrote in message
news:Sj*****************@newssvr27.news.prodigy.ne t...
>I have a Windows Service that accepts requests to convert word documents to
.pdf (it opens the document, prints it via Adobe PDF printer and closes
the document). It does this several times a day and stays running 24/7.

This service is fairly new and has been running for a few months so far
without any problems (although it has probably not processed more than 1
or 2 thousand documents in a row before a windows update or some other
server maintenance requires the server to restart, thus restarting my
service and recycling word). For what it's worth, here are snippets:

The Word object is defined at the class level of the conversion engine:

Private WithEvents objWord As Word.ApplicationClass

Starting up the word object:

Me.objWord = New Microsoft.Office.Interop.Word.ApplicationClass

'Turn off possible UI hangups...
With Me.objWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlert sNone
End With

Opening a document:

Dim objWordDocument As Word.Document
objWordDocument = Me.objWord.Documents.Open(<string>)

Printing a document:

objWordDocument.PrintOut(Background:=False, Copies:=<integer>,
PrintToFile:=<boolean>)

Closing the document:

objWordDocument.Close(Word.WdSaveOptions.wdDoNotSa veChanges)
objWordDocument = Nothing

Shutting down word:

'In case any documents are still open...
For Each objDocument As Word.Document In Me.objWord.Documents
objDocument.Close(Word.WdSaveOptions.wdDoNotSaveCh anges)
Next

Me.objWord.Quit(Word.WdSaveOptions.wdDoNotSaveChan ges)
Me.objWord = Nothing

To date this service has performed without error, but granted it has never
processed more than 10k documents in a row. Perhaps there is a memory
leak in Word and you should recycle your Word application object every
5k - 10k documents or so, or perhaps there is something in the above code
that can assist you.

Derek Hart wrote:
>I am using late bound Microsoft Word integration with a vb.net winforms
application. If I run code such as the following:

Dim objWord As Object
Dim objWrdDoc As Object
Dim count As Integer
Dim filename As String
filename = "c:\temp\mergedata\1.doc"
objWord = CreateObject("Word.Application")
For count = 1 To 10000
objWrdDoc = objWord.Documents.Open(filename)
objWrdDoc.PrintOut(BackGround:=False)
objWrdDoc.Close(wdDoNotSaveChanges)
Next
objWord.Quit(wdDoNotSaveChanges)

This will work with a huge loop, way more than even 10,000. Now in my
actual program I have this code, except much more logic is done to
determine which documents to print (the variable filename is constantly
changing). The objWord object is passed into another routine, and it is
always passed ByRef. All the other logic works fine, but after about
11000 documents, Word crashes. It does not crash with a blue screen, but
it just stops, and I cannot click almost anything in Windows unless I
exit my winforms application, then I can use Windows normally. I have
tried every variation

May 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.