3

I am using Microsoft.Office.Interop.Word.ApplicationClass to read a set of Word documents. I am able to read them fine and all, but I noticed that the process that is used to read these docs never actually ends according to the Windows Task Manager.

From what I have Google'd, there doesn't seem to be anyone else who has this issues, which leads me to believe that I either am doing something fundamentally wrong, or I lack the ability to effectively phrase my issue for a Google search.

I am relatively new with C#, so I suspect the former. Find below the code that I am using to create the instance of the document 'reader'.

private void readDoc(string docPath)
{
    Word.ApplicationClass wordApp = new Word.ApplicationClass();
    object nullObj = System.Reflection.Missing.Value;
    object roObj = true;
    object objFile = docPath;

    try
    {
        Word.Document doc = wordApp.Documents.Open(ref objFile,
            ref nullObj, ref roObj, ref nullObj, ref nullObj, ref nullObj,
            ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj,
            ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject tmpData = Clipboard.GetDataObject();
        string docText = tmpData.GetData(DataFormats.Text).ToString();

          (...)
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Data);
    }
}

The rest of the try block deals with the string docText and doesn't involve wordApp or doc, thus I have not included it in the code segment.

There doesn't seem to be a .Dispose() function for the Word.ApplicationClass so I am at a bit of a loss here.

Edit- Sorry, the implied question here is: How can I end the process programmatically?

3 Answers3

4

You should use the Word.Application class in your code instead of Word.ApplicationClass:

Word.Application wordApp = new Word.Application();

You need to explicitly close your document and Word application when you’re done:

doc.Close();
wordApp.Quit();
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • i am using `WordApp.Quit();` and it's closing the Application... But after i click On **Word** In Task Bare ! my form is a **TopMost** does this has something to do with that, and how to solve this problem...? – KADEM Mohammed Dec 07 '13 at 15:34
3

Try adding the following at the end of readDoc():

wordApp.Quit(ref nullObj, ref nullObj, ref nullObj);

Keplah
  • 954
  • 2
  • 13
  • 26
2

It's a common problem and is described in this KB article.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Interesting. However, it appears that this issue only applies to 2002 and 2003 releases of .NET according to the last section of the article. Either way, thank you for responding with this link. I would upvote, but I lack the 'reputation points' to do so. – Riequa Vetrilo Jun 08 '12 at 21:46
  • @Riequa, the problem still applies to more recent versions of .NET. There are plenty of StackOverflow questions on the subject, for example the following (about Excel, but the problem is the same for any Office application): http://stackoverflow.com/questions/5189379/how-to-end-excel-exe-process – Joe Jun 11 '12 at 16:23