Link to home
Start Free TrialLog in
Avatar of EylonM
EylonM

asked on

Releasing Word object after completion of word automation in C#

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:

oMyword.oword.Quit();
//Marshal.ReleaseComObject(oMyword.oWord);
  while (Marshal.ReleaseComObject(oMyword.oWord) > 0) ;
     oMyword.oWord = null;
     oMyword = null;
Avatar of jimyX
jimyX

try this:

//object junk = System.Reflection.Missing.Value;
....
if(oMyWord != null) {
//oMyWord.Quit(ref junk, ref junk, ref junk);
Marshal.ReleaseComObject(oMyWord);
oMyWord = null;
}
ASKER CERTIFIED SOLUTION
Avatar of Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EylonM

ASKER

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

Thanks
Avatar of EylonM

ASKER

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>>"))
            {
                oMyword.GoToBookmark("rate_includes");
                rtFile.Text = myReservation.cIncludedTerms;
                oMyword.InserFile(rtFile);
                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?
Avatar of EylonM

ASKER

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

public string getDocContentsAsString()
        {
            oWord.Selection.WholeStory();
            oWord.Selection.Copy();
            string docContents = Clipboard.GetText();
            return docContents;

        }