473,396 Members | 1,799 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,396 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 4846
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.