访问COM组件时出错

问题描述 投票:28回答:6

我为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)

我该如何解决这个问题?

c# com ms-word office-2007
6个回答
57
投票

问题解决了!

我之前安装了Office 2010,因此Windows注册表中存在一些不一致之处。要修复它们,请打开qazxsw poi并从错误中找到CLSID。

你会发现第二个错误:

regedit

使用子键:

  • ProxyStubClsid
  • ProxyStubClsid32
  • 类型库

看看HKEY_CLASSES_ROOT\Interface\{00020970-0000-0000-C000-000000000046} 里面的(Default)Version值。

现在找到下面的节点,使用TypeLib值作为(Default)

<TypeLib ID>

作为这个元素的孩子,你会发现不止一个元素,其中一个是第一个注册表的HKEY_CLASSES_ROOT\TypeLib\<TypeLib ID>\<version>。如果你检查其他元素,你会发现它们没有任何意义。删除其他人!!!它解决了!


7
投票

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

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

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


1
投票

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


0
投票

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


0
投票

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

Version


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.