[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Releasing Word object after completion of word automation in C#

Posted on 2010-11-21
5
Medium Priority
?
520 Views
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:

oMyword.oword.Quit();
//Marshal.ReleaseComObject(oMyword.oWord);
  while (Marshal.ReleaseComObject(oMyword.oWord) > 0) ;
     oMyword.oWord = null;
     oMyword = null;
0
Comment
Question by:EylonM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 34183555
try this:

//object junk = System.Reflection.Missing.Value;
....
if(oMyWord != null) {
//oMyWord.Quit(ref junk, ref junk, ref junk);
Marshal.ReleaseComObject(oMyWord);
oMyWord = null;
}
0
 
LVL 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 1000 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;
                  try
                  {
                      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)
                  {
                      MessageBox.Show(appexp.Message);
                      throw appexp;
                  }
                  isOpen = true;
                  return doc;
        }
       
        // Close the doc
       
        object missing = System.Reflection.Missing.Value;
                      try
                      {
                          doc.Close(ref missing, ref missing, ref missing);
                      }
                      catch (Exception closedocexcpt)
                      {
                          MessageBox.Show(closedocexcpt.Message);
                      }
                  }
            doc = null;
           
           
        // Quit
       
        object missing = System.Reflection.Missing.Value;
                      try
                      {
                          app.Quit(ref missing, ref missing, ref missing);
                      }
                      catch (Exception quit)
                      {
                          MessageBox.Show(quit.Message);
                      }
                app = null;
               
               
       
0
 

Author Comment

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

Thanks
0
 

Author Comment

by:EylonM
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>>"))
            {
                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?
0
 

Author Comment

by:EylonM
ID: 34187026
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;

        }

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

650 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