Windows Develop Bookmark and Share   
 index > Windows Forms General > Can't Kill WINWORD.EXE...
 

Can't Kill WINWORD.EXE...

I've posted a message before, but my question wasn't really answered.    I'm using VB.NET to open, print and close a Word Document.  Thing is it leaves an instance of WINWORD.EXE in TaskManager - and it seems nothing will kill it. After a while, there are so many instances, the computer runs out of memory - that's annoying to say the least. 
Any help would really be appreciated.

Here's the code:
Dim oWordApp As New Word.ApplicationClass 
Dim moWordDoc As Word.Document 

oWordApp = New Word.Application 
oWordApp.Visible = False 

moWordDoc = oWordApp.Documents.Add(mFilePath) 
moWordDoc.PrintOut() 

'oWordApp.Documents.Close(False) 
oWordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges) 
'CType(oWordApp, Word._Application).Quit() 'Doesn't work 
oWordApp.Quit() 'Doesn't work either 
moWordDoc = Nothing 
oWordApp = Nothing


*****************************************************************************
I've also tried the following:
            Dim WordProcesses() As Process = Process.GetProcessesByName("WINWORD.EXE")

            Dim WordProcess As Process = Nothing

            For Each WordProcess In WordProcesses
                WordProcess.Kill()
            Next

Neither works.

Thanks,
Denvas
MigrationUser 1  Friday, January 30, 2004 7:36 AM
Hi. Just tried it with Word 2003, just to make sure, and this code works fine:

    Dim oWordApp As New Word.Application
    Dim moWordDoc As Word.Document

    moWordDoc = oWordApp.Documents.Add(mfilePath)
    moWordDoc.PrintOut()
    moWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)

    oWordApp.Quit()
    ' Using the Office 2003 PIA, this isn't required. With
    ' an earlier version, it might be:
    ' System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp)

You had two instantiations in there, so you got two copies of Word loaded. So you were effectively killing one, but leaving the other one in memory. Even without that, depending on which version of Word you're using, and whether you've installed the primary interop assemblies for that version (downloadable for Office XP, included in the box for Office 2003), you may need to call the ReleaseComObject method as shown in the sample. I didn't need to, for Office 2003.
MigrationUser 1  Monday, February 02, 2004 8:44 PM
You're a GENIUS! Thanks.  It works perfectly in Office XP. Problem definitely was the double instance - didn't notice that. I got the code off some forum, and spent the rest of the time figuring out what was wrong.

Thanks a mil,
Denvas
MigrationUser 1  Wednesday, February 04, 2004 8:06 AM
Nope, not a genius. It's just always easier to rethink someone ELSE's code <g>. Glad it all worked out.
MigrationUser 1  Saturday, February 07, 2004 7:14 PM
Hi,

I have a similar problem, but your previous tip didn't help me. I'm running Win2000 with Office2000 and the following code leaves an instance on WINWORD.EXE every time I run it. I would REALLYYYY!!!! appreaciate if somebody could help me our with this one. It just drives me crazy...

Public Function ReadWordFile(ByVal sName As String) As String
        Dim WordApp As New Word.Application
        '   ReplacePoint
        Dim glContext As Integer
        glContext = 929

        Dim sBuffer As String
        Dim objdoc As Word.Document

        objdoc = WordApp.Documents.Open(sName)
        sBuffer = objdoc.Range.Text
        ReadWordFile = sBuffer
        objdoc.Close(False)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp)
        objdoc = Nothing
        WordApp = Nothing
End Function

Thanks in advance,
Denis Stankovski
MigrationUser 1  Monday, April 05, 2004 6:26 PM
I wrote a static class for handling Office 2003 application. It count the number of concurrent user of Word/Excel and kills the app only when there are no users. However, the WINWORD.EXE and EXCEL.EXE are not being killed. Any idea/help would be appreciated.

using System;
using System.IO;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Reflection;

namespace OfficeServer
{
/// <summary>
/// Summary description for OfficeApp.
/// </summary>
public class OfficeApp
{
#region "Public methods"
public static OfficeApp OfficeInstance
{
get
{
return office;
}
}

public Word.Application Word
{
get
{
if ( word == null ) 
{
CreateWord();
}
return word;
}
}

public Excel.Application Excel
{
get
{
if ( excel == null ) 
{
CreateExcel();
}
return excel;
}
}

/// <summary>
/// Disposes Word instance if it's the last one
/// </summary>
public void DisposeWord()
{
lock(this)
{
wordInstancesNum--;
if ( wordInstancesNum > 0 ) return;
if ( word == null ) return;

object falseObj = false,
missingValueObj = Missing.Value;

word.Quit( ref falseObj, ref missingValueObj, ref missingValueObj );
System.Runtime.InteropServices.Marshal.ReleaseComObject(word); 
word = null;
}
}


/// <summary>
/// Disposes Excel instance if it's the last one
/// </summary>
public void DisposeExcel()
{
lock(this)
{
excelInstancesNum--;
if ( excelInstancesNum > 0 ) return;
if ( excel == null ) return;

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); 
excel = null;
}
}

#endregion

#region "Private members"
static private OfficeApp office = new OfficeApp();
private Word.Application word = null;
private static int wordInstancesNum = 0;
private Excel.Application excel = null;
private static int excelInstancesNum = 0;
#endregion

#region "Private methods"
private OfficeApp()
{
}


/// <summary>
/// Creates Word instance if none exists
/// </summary>
private void CreateWord()
{
lock(this)
{
if ( word != null ) return;
word = new Word.Application();
word.Visible = false;
word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
word.FeatureInstall = Microsoft.Office.Core.MsoFeatureInstall.msoFeatureInstallNone;
//word.Options.DefaultFilePath(Word.WdDefaultFilePath.wdUserTemplatesPath) = dirName;
wordInstancesNum++;
}
}


/// <summary>
/// Creates Excel instance if none exists
/// </summary>
private void CreateExcel()
{
lock(this)
{
if ( excel != null ) return;
excel = new Excel.Application();
excel.Visible = false;
excel.DisplayAlerts = false;
excelInstancesNum++;
}
}



#endregion
}
}
MigrationUser 1  Monday, June 21, 2004 10:39 AM

You can use google to search for other answers

Custom Search

More Threads

• How to convert a string into a datetime.
• Composite Interface or MDI??? Suggestions please.
• How to keep graphics in a panel?
• Coding and Naming Conventions
• WebBrowser as Html WYSIWYG editor - Cut and Paste
• The Font Used By Windows Forms Apps
• How to open a Form with Console application?
• Tooltip on a piechart
• Resize docked form when parent form's size is changed
• Form not opening as maximized