简体   繁体   English

如何处理 System.Runtime.InteropServices.COMException (0x800706BA):RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA)

[英]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.我正在开发一个 Windows 应用程序,我在其中操作 Word 应用程序。 More specific, I am opening a Word Document but when I quit it and try to open another Word Document this Error comes out.更具体地说,我正在打开一个 Word 文档,但是当我退出它并尝试打开另一个 Word 文档时,会出现此错误。

How to handle怎么处理

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. System.Runtime.InteropServices.COMException (0x800706BA):RPC 服务器不可用。 (Exception from HRESULT: 0x800706BA) at Microsoft.Office,Word.ApplicationClass.set_Visible(Boolean Prop)** (来自 HRESULT 的异常:0x800706BA)在 Microsoft.Office、Word.ApplicationClass.set_Visible(Boolean Prop)**

If I don't quit the Word Application this error does not come out.如果我不退出 Word 应用程序,则不会出现此错误。

Below I show you the functions that I open and quit the Word Application.下面我将向您展示我打开和退出 Word 应用程序的功能。

    //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. 主要问题是当我退出并尝试打开另一个Word文档时,打开另一个Word文档意味着获取/创建Word应用程序的对象。 In my case wordApp != null , after finalizing the application, so I had to create another Word Application Object and return it for the case. 在我的情况下, wordApp != null最终确定了应用程序后,因此我不得不创建另一个Word应用程序对象并针对情况返回它。

  //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. CloseWordApp()保持不变。

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. 您需要确保COM服务器处于活动状态。 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. 我建议将此类对象引用设置为null,以便稍后我们可以检查应用程序对象是否仍然存在。 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().我们在 .net web 服务中遇到了这个问题,以及其他错误,例如 Word.Documents.Open() 上的“远程过程调用失败”。 i'll list all the things we tried, and finish with the solution.我将列出我们尝试过的所有事情,并以解决方案结束。 we tried:我们尝试了:

  • Make sure RPC service is up.确保 RPC 服务已启动。 Word is not corrupted, opens properly, including the file we were opening. Word 没有损坏,可以正常打开,包括我们打开的文件。
  • restart server and service hosting the web application.重新启动托管 web 应用程序的服务器和服务。
  • Rollback a windows update that occured the same day it stopped working.回滚在停止工作的同一天发生的 windows 更新。
  • 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.我们将代码隔离到第三方应用程序以验证它是导致问题的 open() 方法,并且还使用了不同的文件。 We created a win form app, and consol app.我们创建了一个 win form 应用程序和一个 consol 应用程序。 We ran that small app as win admin, a regular account as well as the account that runs the web app.我们以 win admin、普通帐户以及运行 web 应用程序的帐户运行该小应用程序。
  • We ran procMon.我们运行了 procMon。
  • we did a repair on word.我们对word进行了修复。
  • we installed Office all over, we tried 32 and 64bits version我们全部安装了 Office,我们尝试了 32 位和 64 位版本

Finale solution: we deleted the user profile that runs the web app.最终解决方案:我们删除了运行 web 应用程序的用户配置文件。

4 days to find that out. 4天就知道了。 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.在发布这些行时,我们不确定为什么本地配置文件会创建此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用。 (HRESULT异常:0x800706BA) - System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) WMI RPC服务器不可用。 (来自HRESULT的异常:0x800706BA) - WMI The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) RPC服务器不可用。 (来自HRESULT的异常:0x800706BA)-Excel - The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) - Excel RPC服务器不可用。 连接到远程计算机时(来自HRESULT的异常:0x800706BA) - The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) when connecting to remote computer WMI:RPC服务器不可用。 (尝试连接到远程计算机时抛出HRESULT异常:0x800706BA) - WMI: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) throws when try to connect to remote machine 如何修复“CCertRequest::Submit:RPC 服务器不可用。0x800706ba”错误? - How to fix "CCertRequest::Submit: The RPC server is unavailable. 0x800706ba" error? LDAP 从域外重置密码。网络 C# 错误:RPC 服务器不可用。 (hresult 的异常:0x800706ba) - LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba) 在错误0x800706BA之前捕获RPC锁定 - Catch RPC lock before error 0x800706BA 获取contact.LastName时System.Runtime.InteropServices.COMException(0x800706BE) - System.Runtime.InteropServices.COMException (0x800706BE) when getting contact.LastName InternetExplorer COMException: System.Runtime.InteropServices.COMexception: RPC 服务器不可用 - InternetExplorer COMException: System.Runtime.InteropServices.COMexception: The RPC server is unavailable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM