[英]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:
我们尝试了:
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.