Releasing Word object after completion of word automation in C#

Posted on 2010-11-21
Last Modified: 2013-12-17
I am doing word automation in c# and can't seem to get rid of the word object - both the application and the word get stuck.  I have tried various permutations of the following:

  while (Marshal.ReleaseComObject(oMyword.oWord) > 0) ;
     oMyword.oWord = null;
     oMyword = null;
Question by:EylonM
  • 3
LVL 24

Expert Comment

ID: 34183555
try this:

//object junk = System.Reflection.Missing.Value;
if(oMyWord != null) {
//oMyWord.Quit(ref junk, ref junk, ref junk);
oMyWord = null;

Accepted Solution

Subrat (C++ windows/Linux) earned 250 total points
ID: 34185289
Modify the following code...
In your reference add Microsoft.Office.Interop.Word

Microsoft.Office.Interop.Word.Application app = null;
        Microsoft.Office.Interop.Word.Document doc = null;
        app = new Microsoft.Office.Interop.Word.Application();
        //Open the document
             private Document OpenDoc(object fname, bool format)
                  object missing = System.Reflection.Missing.Value;
                  object ReadOnly = false;
                  object isVisible = false;
                      doc = app.Documents.Open(ref fname, ref missing, ref ReadOnly, ref missing,
                                               ref missing, ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing, ref isVisible,
                                               ref missing, ref missing, ref missing, ref missing);
                  catch (Exception appexp)
                      throw appexp;
                  isOpen = true;
                  return doc;
        // Close the doc
        object missing = System.Reflection.Missing.Value;
                          doc.Close(ref missing, ref missing, ref missing);
                      catch (Exception closedocexcpt)
            doc = null;
        // Quit
        object missing = System.Reflection.Missing.Value;
                          app.Quit(ref missing, ref missing, ref missing);
                      catch (Exception quit)
                app = null;

Author Comment

ID: 34185583
Subrat2009, could you show the code that would call Opendoc please?


Author Comment

ID: 34186406
I discovered the source of the word hanging.  Here is the code that is causing the problem:

RichTextBox rtFile = new RichTextBox();
            string cContents = oMyword.getDocContentsAsString();
            if (cContents.Contains("<<rate_includes>>"))
                rtFile.Text = myReservation.cIncludedTerms;
                oMyword.FindAndReplace("<<rate_includes>>", "");

These lines are causing word to hang when I try to release it:
string cContents = oMyword.getDocContentsAsString();
            if (cContents.Contains("<<rate_includes>>"))

Why?  Is cContents somehow a copy of the word object that isn't being released?

Author Comment

ID: 34187026
Continuing from above - where the function below seems to be causing the problem:

public string getDocContentsAsString()
            string docContents = Clipboard.GetText();
            return docContents;



Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help embedding an image as HTML in my application 3 48
Data is not showing from images 15 37
What .NET URL re-routing tool did I use? 2 36
SQL Login 17 38
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now