473,856 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\merged ata\1.doc"
objWord = CreateObject("W ord.Application ")
For count = 1 To 10000
objWrdDoc = objWord.Documen ts.Open(filenam e)
objWrdDoc.Print Out(BackGround: =False)
objWrdDoc.Close (wdDoNotSaveCha nges)
Next
objWord.Quit(wd DoNotSaveChange s)

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.Releas eComObject(o)
System.GC.Colle ct()
System.GC.WaitF orPendingFinali zers()

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 4888
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.Applicatio nClass

Starting up the word object:

Me.objWord = New Microsoft.Offic e.Interop.Word. ApplicationClas s

'Turn off possible UI hangups...
With Me.objWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = Microsoft.Offic e.Interop.Word. WdAlertLevel.wd AlertsNone
End With

Opening a document:

Dim objWordDocument As Word.Document
objWordDocument = Me.objWord.Docu ments.Open(<str ing>)

Printing a document:

objWordDocument .PrintOut(Backg round:=False, Copies:=<intege r>,
PrintToFile:=<b oolean>)

Closing the document:

objWordDocument .Close(Word.WdS aveOptions.wdDo NotSaveChanges)
objWordDocument = Nothing

Shutting down word:

'In case any documents are still open...
For Each objDocument As Word.Document In Me.objWord.Docu ments
objDocument.Clo se(Word.WdSaveO ptions.wdDoNotS aveChanges)
Next

Me.objWord.Quit (Word.WdSaveOpt ions.wdDoNotSav eChanges)
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\merged ata\1.doc"
objWord = CreateObject("W ord.Application ")
For count = 1 To 10000
objWrdDoc = objWord.Documen ts.Open(filenam e)
objWrdDoc.Print Out(BackGround: =False)
objWrdDoc.Close (wdDoNotSaveCha nges)
Next
objWord.Quit(wd DoNotSaveChange s)

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.comw rote in message
news:Sj******** *********@newss vr27.news.prodi gy.net...
>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.Applicatio nClass

Starting up the word object:

Me.objWord = New Microsoft.Offic e.Interop.Word. ApplicationClas s

'Turn off possible UI hangups...
With Me.objWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = Microsoft.Offic e.Interop.Word. WdAlertLevel.wd AlertsNone
End With

Opening a document:

Dim objWordDocument As Word.Document
objWordDocument = Me.objWord.Docu ments.Open(<str ing>)

Printing a document:

objWordDocument .PrintOut(Backg round:=False, Copies:=<intege r>,
PrintToFile:=<b oolean>)

Closing the document:

objWordDocument .Close(Word.WdS aveOptions.wdDo NotSaveChanges)
objWordDocument = Nothing

Shutting down word:

'In case any documents are still open...
For Each objDocument As Word.Document In Me.objWord.Docu ments
objDocument.Clo se(Word.WdSaveO ptions.wdDoNotS aveChanges)
Next

Me.objWord.Quit (Word.WdSaveOpt ions.wdDoNotSav eChanges)
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\merged ata\1.doc"
objWord = CreateObject("W ord.Application ")
For count = 1 To 10000
objWrdDoc = objWord.Documen ts.Open(filenam e)
objWrdDoc.Print Out(BackGround: =False)
objWrdDoc.Close (wdDoNotSaveCha nges)
Next
objWord.Quit(wd DoNotSaveChange s)

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
8349
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 but that's the gist of it.) I have everything set up, and it seems to work except that setting Cancel to true and returning from my c# method appears to do nothing. Word still closes. Am I doing something wrong? private void...
1
3045
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) w.Application.Documents.Open("test.doc") With w.Application.Selection
0
2185
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 formatting. ---------------------------------------------------------------------- Dim w As New Microsoft.Office.Interop.Word.Application Dim d As Microsoft.Office.Interop.Word.Document = w.Documents.Add
7
10968
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 this screen below. Please help, thanks in advance... Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify...
5
2999
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 = word.Documents.Open(WordFileName.ToString) doc.Activate() word.Visible = True This code works on the development machine, but does not work on a test machine. The test machine is set up to use Office 2003, same as the development one.
0
3999
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 WordDoc.Close(ref _newMissing, ref _newMissing, ref _newMissing); WordApp.Application.Quit(ref _notTrue, ref _newMissing, ref _newMissing);
14
5306
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
11456
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
0
2024
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: =============================================================================================== Error de servidor en la aplicación '/APPWebApp'. -------------------------------------------------------------------------------- ?
1
7882
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 Microsoft.Office.Interop.Word.Document Document = Word.Documents.Open(Filename)
0
9919
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11061
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10699
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10787
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.