Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word ApplicationClass issue

Tags:

c#

.net

ms-word

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?

like image 764
Riequa Vetrilo Avatar asked May 14 '26 18:05

Riequa Vetrilo


1 Answers

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();
like image 74
Douglas Avatar answered May 17 '26 08:05

Douglas