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

M

mac

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.
 
O

old man

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
 
O

old man

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

old man said:
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

mac said:
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.
 
I

Iulia Lucaciu

This worked great! I tried using garbage collection forcebily and it would not shut down the winword.exe process at all.

Added a bit of cleanup code and it worked perfectly:

Private Shared Sub cleanUp()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

'if the release does not work, try some more stuff
Dim pArry() As Process = Process.GetProcesses()

For Each p As Process In pArry

Dim s As String = p.ProcessName
s = s.ToLower()
If (s.CompareTo("winword") = 0) Then
p.Kill()
End If
Next

End Sub
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.
On Tuesday, July 10, 2007 1:24 PM oldma wrote:
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

"mac" wrote:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top