Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Microsoft.Office.Interop.Word create new instance of winword.exe

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mac - 10 Jul 2007 17:26 GMT
hi,

I have created a windows service in C# that reads data from word document
and fills in sql table. This service runs every two minutes. The issue is
that it creates new instance of winword.exe everytime it runs. If i see task
manager there is multiple instance of winword.exe. That way my system crashes
after creating 9-10 process. How can i avoid that. Here is the code

word.ApplicationClass a = new
Microsoft.Office.Interop.Word.ApplicationClass();

word.Document d = a.Documents.Open(ref templateLocation, ref missing, ref
missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing);

/* process data

*/
if (d != null)

{

d.Close(ref saveChanges, ref missing, ref missing);

d = null;

}
if (a != null)

{

a.Quit(ref missing, ref missing, ref missing);

a = null;

}

GC.Collect();



I also tried to read the existing process by using

a = Marshal.GetActiveObject("Word.Application") as word.ApplicationClass;

but it throws an exception when i try to process the document for this
application class..

I also tried to create the word application in the constructor of service.
But when i try to open the document using
d = a.Documents.Open(ref templateLocation, ref missing, ref missing, ref
missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing);

It just exists the service even though i have it in try catch block with
comexception.
old man - 10 Jul 2007 18:24 GMT
Hi,

You can use this code to close all running instances of Word:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;

namespace WindowsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           Process[] pArry = Process.GetProcesses();

           foreach (Process p in pArry)
           {
               string s = p.ProcessName;
               s = s.ToLower();
               if (s.CompareTo("winword") == 0)
               {
                   p.Kill();
               }
           }

       }
   }
}

I modified the code I found on codeguru site from pareshgh. I ran it and it
stopped Word. Really closing Word (and Excel) programmically has been a
challenge for a long time. Some programs would reboot the OS every few hours
to really clean up the running instances of Word/Excel. But this method works
fine.

The above code will kill all instances of Word. For more information on
starting Word and C# you can look at: http://support.microsoft.com/kb/316126

old man

> hi,
>
[quoted text clipped - 55 lines]
> It just exists the service even though i have it in try catch block with
> comexception.
old man - 11 Jul 2007 16:06 GMT
Hi,

I noticed in a wonderful book by Andrew Whitechapel (MS . Net Development
for MS Office) that he ends his code like (where he opened word with
 private word.application wd
....
wd = new word.application();

he has a shutdown routine that includes:

wd.Quit(ref missing, ref missing, ref missing);
wd = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

This invokes the Garbage Collecter and releases the RCW (a wrapper that
allows managed clients (.net) to invoke COM (office programs) methods.
Calling it twice covers rare situations to really force a cleanup. For more
information on what is going on you can read Chapter 2 of Whtechapel's book.

old man

> Hi,
>
[quoted text clipped - 107 lines]
> > It just exists the service even though i have it in try catch block with
> > comexception.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.