none
Too many threads of Word in Task Manager Memory after running my code. ?ow to delete Word Application object? RRS feed

  • Question

  • Hi, I'm using C# in VS2017 and Word 2016 on my Windows 10 PC.

    Every time I run my program, in task manager it would leaves tons of copies Word.  It doesn't take up the CPU time but they take up quite a bit of memory.  Below is my code.  Appricated if someone can tell me what additional steps that I nee to do to solve this.  Thank you 

           private static void DocToDocx(string path)
            {
                Application word = new Application();
                Document document = new Document();
                try
                {
                    if (path.ToLower().EndsWith(".doc"))
                    {
                        var sourceFile = new FileInfo(path);
                        document = word.Documents.Open(sourceFile.FullName);
    
                        string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
                        document.SaveAs2(newFileName, WdSaveFormat.wdFormatXMLDocument,
                                         CompatibilityMode: WdCompatibilityMode.wdCurrent);
                        document.Close();            
    
                        if (word != null)
                        { 
                            word.ActiveDocument.Close();
                            word.Quit();
                        }
                            File.Delete(path);
                    }
                }
                catch(Exception ex)
                { var err = ex.ToString(); }
                finally
                {
                    if (word != null)
                    {
                        word = null;
                        GC.Collect();
                    }
      
    
                }
            }


    Thank you

    Friday, August 4, 2017 5:32 PM

All replies

  • You create 2 instances of Document.

    You can change it to :

     private static void DocToDocx(string path)
            {
                Word.Application word = new Word.Application();
                try
                {
                    if (path.ToLower().EndsWith(".doc"))
                    {
                        var sourceFile = new FileInfo(path);
                        Word.Document document = word.Documents.Open(sourceFile.FullName);
    
                        string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
                        document.SaveAs2(newFileName, Word.WdSaveFormat.wdFormatXMLDocument,
                                         CompatibilityMode: Word.WdCompatibilityMode.wdCurrent);
                        document.Close();
    
                        if (word != null)
                        {                      
                            word.Quit();
                        }
                        System.IO.File.Delete(path);
                    }
                }
                catch (Exception ex)
                {
                    var err = ex.ToString();
                }
                finally
                {
                    if (word != null)
                    {
                        word = null;
                        GC.Collect();
                    }
                }
            }

    Friday, August 4, 2017 6:48 PM
  • Hi I modified per your suggestion but the same problem still exist.  Thank you.  Why doesn't this Application class comes with Delete method?

            private static void DocToDocx(string path)
            {
                Application word = new Application();
                //Document document = new Document();
                try
                {
                    if (path.ToLower().EndsWith(".doc"))
                    {
                        var sourceFile = new FileInfo(path);
                        Document document = word.Documents.Open(sourceFile.FullName);
    
                        string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
                        document.SaveAs2(newFileName, WdSaveFormat.wdFormatXMLDocument,
                                         CompatibilityMode: WdCompatibilityMode.wdCurrent);
                        document.Close();
    
                        if (word != null)
                        { 
                            //word.ActiveDocument.Close();
                            word.Quit();
                        }
    
                        File.Delete(path);
                    }
                }
                catch(Exception ex)
                { var err = ex.ToString(); }
                finally
                {
                    if (word != null)
                    {
                        word = null;
                        GC.Collect();
                    }
                   
    
                }
            }


    Thank you

    Friday, August 4, 2017 8:19 PM
  • It works on my OS (Windows 10)

    , but I had to add

    word.Quit();

    in the catch(Exception ex)

    because if I launch it twice the file is not found (File.Delete) and Word is not killed.


    Friday, August 4, 2017 9:24 PM
  • Hi laJasmine,

    According to your question is more related to word, I will move it to Word for Developers forum for suitable support.

    This forum discuss and ask the C# programming language, IDE, libraries, samples and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 7, 2017 6:43 AM
  • Hi Wendy,

    Isn't this a C# question instead of a Word Developer?

    Thank you.


    Thank you

    Thursday, August 10, 2017 6:22 PM
  • Hi lajasmine,

    Have you tried Castorix31's suggestion? What's your current issue? If it has been solved, I would suggest you mark the helpful reply as answer or provide your solution and mark as answer to close this thread. If not, please feel free to let us know your current issue.

    Best Regards,

    Terry

    Friday, August 11, 2017 3:19 AM