无法获取 OLEObject class - Excel 互操作的 Object 属性

Unable to get the Object property of the OLEObject class - Excel Interop

我用谷歌搜索了这个问题,但未能找到解决方案。如果文件保存为 .xls 而不是 .xlsm,则此代码有效。我正在使用 Office 2013(32 位)。

我编写了一个暴露于 COM 的 C# class 库。 excel 工作簿实例化一个 object 并传入对当前工作簿的引用。 object 然后调用 excel object 并在第一个 sheet.

上更新 Label1 的标题

我已经打开了所有信任中心设置(它适用于 .xls 文件)并且 .xlsm 文件无论如何都不受保护。

我尝试了几种不同的更新标题的方法,我确定我使用的是 OLEObjects - 但它仍然会抛出 Unable to get the Object property of the OLEObject class.

//Method 1
OLEObject label = this._currentBook.ActiveSheet.OLEObjects(1);
label.Object.Caption = "New text";
//
//Method 2
int index = -1;
foreach (OLEObject obj in this._currentBook.ActiveSheet.OLEObjects)
{ 
  if (obj.Name.ToUpper() == "LABEL1") 
  {
    index = obj.Index;
  }
}
if (index >= 0) 
    this._currentBook.ActiveSheet.OLEObjects(1).Object.Caption = "Some text";
//
//Method 3
this._currentBook.ActiveSheet.OleObjects("Label1").Object.Caption = "Different text";

此代码必须在 class 库中进行,因为这是我需要遵守的业务规则。 excel vba 仅实例化我的 class object,托管代码完成其余工作。

解决方案是从您的 %APPDATA% 和 %TEMP% 中删除所有 .exd 文件,因为...

This issue occurs because some scriptable controls are made obsolete in Office 2013 for security reasons. This is by design, and these errors are expected. These scriptable controls are disabled by using a version-specific kill-bit that only applies to these controls, and this only happens when they are used in a document. We recommend that you do not try to embed scriptable controls directly into documents, because this behavior may reduce system security

感谢 yay_excel 对我的帮助。这是帮助我的页面:

http://www.excelforum.com/excel-programming-vba-macros/1060133-run-time-error-1004-on-certain-machines.html

这又指向这篇 MSDN 知识库文章: MSDN