当我尝试初始化Interop.Word.Application时,为什么会出现COMException?

时间:2015-11-24 06:05:45

标签: c# exception ms-word

我试图从Windows 8.1通用应用程序创建,打开,读取/写入.docx文件。我使用的是Visual Studio 2013。

当我初始化Interop.Word.Application

Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();

我收到错误

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code

Additional information: Creating an instance of the COM component with CLSID 
{000209FF-0000-0000-C000-000000000046} using CoCreateInstanceFromApp failed due 
to the following error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)). Please make sure your COM object is in the 
allowed list of CoCreateInstanceFromApp.

我环顾四周,找不到符合我问题的任何东西。我因为权限不足而看到了这个例外,但是我以管理员身份运行,没有运气。

感谢任何帮助。如果您需要更多信息,请与我们联系。

编辑1

创建此应用时,我确实做了一些奇怪的事情。我无法以过去的方式添加对Microsoft.Office.Interop的引用,因此我按照我在网上找到的一些说明进行了添加,并通过指向

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Word.dll

我不确定这是否有帮助,但这是我做的唯一看起来不正常的事情。

2 个答案:

答案 0 :(得分:2)

问题如下(摘自异常消息):

  

请确保您的COM对象在   允许的CoCreateInstanceFromApp列表。

Windows应用商店应用无法加载任意COM对象。商店/通用应用只能使用limited (built-in) set的COM和Win API。并且Microsoft Office不在允许的COM对象列表中。

那你有什么选择?你可以

  • 改为创建桌面应用
  • 将文档处理移至服务器端(如果您要使用COM互操作,则会出现一系列不同的问题),
  • 在服务器上使用基于OpenXML的方法(OpenXML SDK也不能在Store App中使用)
  • 编写您自己的代码,直接修改OpenXML包(例如使用XDocument
  • 使用Brokered Windows Runtime Components for side-loaded Windows Store apps(在处理Office应用程序时,这可能不是一个简单的选择)

答案 1 :(得分:0)

您是否尝试在x86模式下编译解决方案?可能这些COM对象在您的安装中是32位,如果您从64位计算机上的“所有CPU”可执行文件中调用它们,则会失败。

相关问题