C# 访问 COM 组件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12957595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error accessing COM components
提问by Victor
I built an add-in for Microsoft Office Word. There isn't an issue using the add-in when Word is ran as Administrator, but when it's not ran as an Administrator, there are two common exceptions accessing Ribbon elements.
我为 Microsoft Office Word 构建了一个加载项。当 Word 以管理员身份运行时,使用加载项没有问题,但当它不是以管理员身份运行时,访问功能区元素有两个常见的异常。
The first Exception:
第一个异常:
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)
This error occurs when the Control is invalidated by the following code:
当以下代码使控件无效时,会发生此错误:
ribbon.InvalidateControl("control-id");
And the second Exception:
第二个例外:
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()
This error occurs on the last line of the following code:
此错误发生在以下代码的最后一行:
object wdStory = Word.WdUnits.wdStory;
object wdMove = Word.WdMovementType.wdMove;
WrdApp.Selection.EndKey(ref wdStory, ref wdMove)
How can I fix this problem?
我该如何解决这个问题?
采纳答案by Victor
Problem solved!
问题解决了!
I have previously installed Office 2010, so there are some inconsistences in Windows Registry.
To fix them, open the regeditand find for the CLSID from the error.
我之前安装过Office 2010,所以Windows Registry有一些不一致的地方。要修复它们,请打开regedit并从错误中查找 CLSID。
You will find something like that for the second error:
对于第二个错误,您会发现类似的内容:
HKEY_CLASSES_ROOT\Interface\{00020970-0000-0000-C000-000000000046}
With the subkeys:
使用子键:
- ProxyStubClsid
- ProxyStubClsid32
- TypeLib
- 代理存根类
- ProxyStubClsid32
- 类型库
Take a look at the (Default)and Versionvalues inside of TypeLib.
看看里面的(Default)和Version值TypeLib。
Now find the node below, using the (Default)value as <TypeLib ID>.
现在找到下面的节点,使用(Default)值作为<TypeLib ID>。
HKEY_CLASSES_ROOT\TypeLib\<TypeLib ID>\<version>
HKEY_CLASSES_ROOT\TypeLib\<TypeLib ID>\<version>
As child of this elements you will find more than one element, one of then is the Versionof the first registry. If you inspect the others elements, you will find that they point to nothing. Remove the others!!!It's solved!
作为此元素的子元素,您会发现多个元素,其中一个是Version第一个注册表的 。如果您检查其他元素,您会发现它们指向任何内容。其他的删掉!!!解决了!
回答by Cicekfidan
Just install Office 2010 / MS word / .NET Programmability Support.
只需安装 Office 2010 / MS word / .NET Programmability Support。
回答by Nikita G.
I started getting the same exception after upgrading to the latest Office version. I tried a number of suggested fixes including cleaning up the registry in a way similar to what @Victor described.
升级到最新的 Office 版本后,我开始遇到相同的异常。我尝试了许多建议的修复,包括以类似于@Victor 描述的方式清理注册表。
What eventually helped (even though it might have been a combination of factors) was 'repairing' the installation:
最终帮助(即使它可能是多种因素的组合)是“修复”安装:
Programs and Features→ latest Officeversion → Repair.
Programs and Features→ 最新Office版本 → Repair。
回答by Andrew
@Victor You got me headed on the right path. My issue was not multiple values in the registry but rather, a missing value that Office365 never added. Thanks to you I was able to find my own resolution. A thousand thousand thanks.
@Victor 你让我走上了正确的道路。我的问题不是注册表中的多个值,而是 Office365 从未添加的缺失值。多亏了你,我才能找到自己的解决方案。一千多谢。
回答by Vaibhav Verma
I got the same problem today with VS2015 and Office 2013. Changing the Platform target to x64 worked for me.
我今天在 VS2015 和 Office 2013 上遇到了同样的问题。将平台目标更改为 x64 对我有用。
回答by Joseph Warero
The office repair worked for me. In my case I had installed Project which I believe altered the config and was unable to call up a procedure to migrate data from an excel workbook to SQL
办公室维修对我有用。在我的情况下,我安装了 Project,我认为它改变了配置,并且无法调用将数据从 excel 工作簿迁移到 SQL 的过程

