简体   繁体   中英

How to handle System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

I am developing a Windows application where I manipulate Word Application. More specific, I am opening a Word Document but when I quit it and try to open another Word Document this Error comes out.

How to handle

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) at Microsoft.Office,Word.ApplicationClass.set_Visible(Boolean Prop)**

If I don't quit the Word Application this error does not come out.

Below I show you the functions that I open and quit the Word Application.

    //function to open word Document located in a specific path
        public static void openWordDocument(string fileName)
        {
            try
            {
                wordApplication.Visible = true;
                string filePath = myPath + fileName;
                WordApi.Document docx = wordApplication.Documents.Open(filePath);
            }
            catch (Exception ex)
            {
                MyLogger.Error(ex.ToString());
            }
        }



//function to quit wordApplication 
  public static void CloseWordApp() {

            try {
                Object wordAppObject = Marshal.GetActiveObject("Word.Application");
                WordApi.Application wordApp = (WordApi.Application)wordAppObject;  //cast Object to its actual type
                wordApp.Quit();
            }
            catch (Exception ex) {
                 MyLogger.Error(ex.ToString());
            }


I finally figured it out what is the problem. The main problem was that when I quit it and try to open another Word Document,which opening another Word Document means get/create an Object of Word Application. In my case wordApp != null , after finalizing the application, so I had to create another Word Application Object and return it for the case.

  //open word Document located in a specific path
    public static void openWordDocument(string fileName)
    {
        try
        {
            wordApplication = createWordApplicationObject(wordApplication);
            wordApplication.Visible = true;
            string filePath = myPath + fileName;
            WordApi.Document docx = wordApplication.Documents.Open(filePath);
        }
        catch (Exception ex)
        {
            MyLogger.Error(ex.ToString());
        }
    }
private static WordApi.Application createWordApplicationObject(WordApi.Application wordApp)
    {
        WordApi.Application wordAppFirstTime;
        WordApi.Application wordApp1;
        if (wordApp == null)
        {
            wordAppFirstTime = new WordApi.Application();
            return wordAppFirstTime;

        }
        else
        {
            wordApp1 = new WordApi.Application();
            return wordApp1;
        }

    }

With CloseWordApp() remain the same.

Most probably the exception is fired by the following line of code:

wordApplication.Visible = true;

You need to make sure the COM server is alive. Because after quitting the object becomes unavailable. I'd suggest setting such object references to null, so later we could check whether the application object is still alive. For example:

try
{
    if (wordApplication == null)
    {
        wordApplication = new Word.Application();
    }
    wordApplication.Visible = true;
    string filePath = myPath + fileName;
    WordApi.Document docx = wordApplication.Documents.Open(filePath);
}
catch (Exception ex)
{
    MyLogger.Error(ex.ToString());
}

I wanted to add a solution that works for me. We had this issue in a .net web service, along with other errors, like "the remote procedure call failed" on Word.Documents.Open(). i'll list all the things we tried, and finish with the solution. we tried:

  • Make sure RPC service is up. Word is not corrupted, opens properly, including the file we were opening.
  • restart server and service hosting the web application.
  • Rollback a windows update that occured the same day it stopped working.
  • Uninstalled the antivirus software.
  • We isolated the code to a third party app to validate it was the open() method that caused the problem, and using different files as well. We created a win form app, and consol app. We ran that small app as win admin, a regular account as well as the account that runs the web app.
  • We ran procMon.
  • we did a repair on word.
  • we installed Office all over, we tried 32 and 64bits version

Finale solution: we deleted the user profile that runs the web app.

4 days to find that out. I'd thought i'd share my paine with the world. lol

while posting these lines, we are not sure why the local profile created this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM