locked
How to close Microsoft Word Microsoft.Office.Interop.Word object when I am done? RRS feed

  • Question

  • I am using Microsoft Word Microsoft.Office.Interop.Word to save the word document to PDF.
    At the end of it, I set the object to null.
    But when I look at Task Manager, I see many WINWORD.EXE.

    How can I property close the Microsoft Word Microsoft.Office.Interop.Word object when I am done?

    Thank you

    object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
                oWord.Selection.InlineShapes.AddPicture(path, false, true);
                oWord.ActiveDocument.ExportAsFixedFormat(txtFolder.Text + "\\" + fileName + ".pdf", WdExportFormat.wdExportFormatPDF, false, 0, 0, 1, 1, 0, true, true, 0, true, true, false); //wdExportFormatPDF =17, wdExportOptimizeForPrint=0, wdExportAllDocument=0, wdExportDocumentContent=0, wdExportCreateNoBookmarks =0
                oDoc = null;
                oWord = null;

    • Moved by Kristin Xie Wednesday, December 16, 2015 3:08 AM move to better forum
    Monday, December 14, 2015 11:04 PM

Answers

  • Try adding the following lines at the end:

    Marshal.ReleaseComObject (oDoc);
    Marshal.ReleaseComObject (oWord);


    • Marked as answer by aujong Wednesday, December 16, 2015 2:36 PM
    Monday, December 14, 2015 11:34 PM

All replies

  • Try adding the following lines at the end:

    Marshal.ReleaseComObject (oDoc);
    Marshal.ReleaseComObject (oWord);


    • Marked as answer by aujong Wednesday, December 16, 2015 2:36 PM
    Monday, December 14, 2015 11:34 PM
  • Use Application.Quit method which has definition as below.

            // Summary:
            //     Quits Microsoft Word and optionally saves or routes the open documents.
            //
            // Parameters:
            //   SaveChanges:
            //     Optional Object. Specifies whether Word saves changed documents before quitting.
            //     Can be one of the Microsoft.Office.Interop.Word.WdSaveOptions constants.
            //
            //   OriginalFormat:
            //     Optional Object. Specifies the way Word saves documents whose original format
            //     was not Word Document format. Can be one of the Microsoft.Office.Interop.Word.WdOriginalFormat
            //     constants.
            //
            //   RouteDocument:
            //     Optional Object. True to route the document to the next recipient. If the document
            //     doesn't have a routing slip attached, this argument is ignored.
            [DispId(1105)]
            void Quit(ref System.Object SaveChanges, ref System.Object OriginalFormat, ref System.Object RouteDocument);

    invoke like below

    _app.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges,

    Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument);


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Tuesday, December 15, 2015 1:44 AM
  • Hi aujong,

    This forum is discussing about the C# development, as your issue is more related to the Word development, we help you move it to the Word for Developers forum for better support.

    Thank you for your understanding.

    Best Regards,

    Albert Zhang

    Wednesday, December 16, 2015 3:06 AM
  • Thank you all.

    The following codes work

    oDoc.Close(false);
    oDoc = null;
    oWord.Quit(false);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
    oWord = null;
    

    Wednesday, December 16, 2015 2:37 PM
  • Hello,

    The Quit method of the Application class quits Microsoft Word and optionally saves or routes the open documents.

    Also I'd suggest releasing all underlying COM objects instantly to make sure the Word process is finished successfully (removed from the list of running processes). Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release a Word object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

    Wednesday, December 16, 2015 5:38 PM