473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Word Interop

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
2 3206
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AP | last post by:
Hi, I'm trying to use c# to pop up a dialog box when a user attempts to close word to prompt them if they want to exit or cancel (obviously other stuff needs to happen based on their selection...
1
by: Bernd Muent | last post by:
Hi together, I am using the following code in Visual Basic to open Word or Excel applications: Word: Dim w As Word.Application w = CType(CreateObject("Word.application"), Word.Application)...
0
by: Nitin | last post by:
How can I create and use Tables in Microsoft word document through VB .net? I've been using the following code for printing an envelope using paragraphs. Tables have to be used for better...
7
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed...
5
by: Bob | last post by:
I'm getting error 8007007e when executing the following code. Dim word As New Microsoft.Office.Interop.Word.Application Dim doc As Microsoft.Office.Interop.Word.Document doc =...
0
by: Alan T | last post by:
I tried to close the word document object and word application: private Interop.Word.Application WordApp; private Interop.Word.Document WordDoc; // close word and quit word application ...
14
by: Alan T | last post by:
These are the codes I created a document: _WordApp = new Interop.Word.Application(); _WordApp.Visible = false; _WordDoc = new Document(); _Missing = System.Reflection.Missing.Value;
2
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
0
by: SergioT | last post by:
hi I m getting an error when I run my application on the server, that error doesn't ocur when I run the application on the development computer the error is:...
1
by: =?ISO-8859-1?Q?S=F8ren?= | last post by:
Hi guys I got the following code: ------------------------------------------------------- Dim Word As New Microsoft.Office.Interop.Word.Application Dim Document As...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.