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.o Word);
while (Marshal.ReleaseComObject( oMyword.oW ord) > 0) ;
oMyword.oWord = null;
oMyword = null;
oMyword.oword.Quit();
//Marshal.ReleaseComObject
while (Marshal.ReleaseComObject(
oMyword.oWord = null;
oMyword = null;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Subrat2009, could you show the code that would call Opendoc please?
Thanks
Thanks
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.getDocContentsAsSt ring();
if (cContents.Contains("<<rat e_includes >>"))
{
oMyword.GoToBookmark("rate _includes" );
rtFile.Text = myReservation.cIncludedTer ms;
oMyword.InserFile(rtFile);
oMyword.FindAndReplace("<< rate_inclu des>>", "");
}
These lines are causing word to hang when I try to release it:
string cContents = oMyword.getDocContentsAsSt ring();
if (cContents.Contains("<<rat e_includes >>"))
Why? Is cContents somehow a copy of the word object that isn't being released?
RichTextBox rtFile = new RichTextBox();
string cContents = oMyword.getDocContentsAsSt
if (cContents.Contains("<<rat
{
oMyword.GoToBookmark("rate
rtFile.Text = myReservation.cIncludedTer
oMyword.InserFile(rtFile);
oMyword.FindAndReplace("<<
}
These lines are causing word to hang when I try to release it:
string cContents = oMyword.getDocContentsAsSt
if (cContents.Contains("<<rat
Why? Is cContents somehow a copy of the word object that isn't being released?
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;
}
public string getDocContentsAsString()
{
oWord.Selection.WholeStory
oWord.Selection.Copy();
string docContents = Clipboard.GetText();
return docContents;
}
//object junk = System.Reflection.Missing.
....
if(oMyWord != null) {
//oMyWord.Quit(ref junk, ref junk, ref junk);
Marshal.ReleaseComObject(o
oMyWord = null;
}