访问COM组件时出错

时间:2012-10-18 14:55:34

标签: c# com ms-word office-2007

我为Microsoft Office Word构建了一个加载项。当Word以管理员身份运行时使用加载项没有问题,但是当它不以管理员身份运行时,访问功能区元素有两个常见的例外。

第一个例外:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.IRibbonUI'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000C03A7-0000-0000-C000-000000000046}' failed due to the following error:  could not be found. (Exception from HRESULT: 0x80030002 (STG_E_FILENOTFOUND)).
   at Microsoft.Office.Core.IRibbonUI.InvalidateControl(String ControlID)

通过以下代码使控件无效时会发生此错误:

ribbon.InvalidateControl("control-id");

第二个例外:

Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error:  could not be found. (Exception from HRESULT: 0x80030002 (STG_E_FILENOTFOUND)).
   at Microsoft.Office.Interop.Word.ApplicationClass.get_Selection()

以下代码的最后一行发生此错误:

object wdStory = Word.WdUnits.wdStory;
object wdMove = Word.WdMovementType.wdMove;
WrdApp.Selection.EndKey(ref wdStory, ref wdMove)

如何解决此问题?

6 个答案:

答案 0 :(得分:55)

问题解决了!

我以前安装过Office 2010,因此Windows注册表中存在一些不一致之处。 要解决这些问题,请打开regedit并从错误中找到CLSID。

你会发现第二个错误:

HKEY_CLASSES_ROOT\Interface\{00020970-0000-0000-C000-000000000046}

使用子键:

  • ProxyStubClsid
  • ProxyStubClsid32
  • 类型库

查看(Default)内的VersionTypeLib值。

现在使用(Default)<TypeLib ID>找到下面的节点。

HKEY_CLASSES_ROOT\TypeLib\<TypeLib ID>\<version>

作为这些元素的子元素,您将找到多个元素,其中一个元素是第一个注册表的Version。如果你检查其他元素,你会发现它们没有任何意义。 删除其他人!!! 它已经解决了!

答案 1 :(得分:7)

升级到最新的Office版本后,我开始获得相同的异常。我尝试了一些建议的修复程序,包括以类似于@Victor描述的方式清理注册表。

最终帮助(即使它可能是多种因素的组合)是'修复'安装:

程序和功能→最新 Office 版本→修复

答案 2 :(得分:1)

办公室维修对我有用。在我的情况下,我安装了Project,我相信更改了配置,无法调用将数据从excel工作簿迁移到SQL的过程

答案 3 :(得分:0)

只需安装Office 2010 / MS word / .NET可编程性支持。

答案 4 :(得分:0)

@Victor你让我走上了正确的道路。我的问题不是注册表中的多个值,而是Office365从未添加的缺失值。多亏了你,我能够找到自己的解决方案。千万感谢。

Details are here for anyone still looking for a resolution.

答案 5 :(得分:0)

我今天在VS2015和Office 2013上遇到了同样的问题。将平台目标更改为x64对我有用。