简体   繁体   English

无法获取OLEObject类的Object属性-Excel Interop

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

I have googled this problem and have not been able to come up with a solution. 我已经用谷歌搜索了这个问题,还没有找到解决方案。 This code works if the file is saved as an .xls but not .xlsm. 如果文件另存为.xls而不是.xlsm,则此代码有效。 I am using Office 2013 (32-bit). 我正在使用Office 2013(32位)。

I have written a C# class library that is COM exposed. 我已经编写了一个COM公开的C#类库。 The excel workbook instantiates an object and passes in a reference to the current workbook. excel工作簿实例化一个对象,并传递对当前工作簿的引用。 The object then calls the excel object and updates Label1's caption on the first sheet. 然后,该对象调用excel对象,并在第一张纸上更新Label1的标题。

I have opened up all trust center settings (it works with .xls files) and the .xlsm file is not protected in anyway. 我已经打开了所有信任中心设置(它与.xls文件一起使用),并且无论如何该.xlsm文件都不受保护。

I have tried a couple different methods of updating the caption, I have made sure I am using OLEObjects - but it still throws a Unable to get the Object property of the OLEObject class . 我已经尝试了几种不同的方法来更新标题,我已经确保我正在使用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";

This code has to take place in the class library as that is the business rules I need to comply with. 该代码必须在类库中进行,因为这是我需要遵守的业务规则。 The excel vba merely instantiates my class object and the managed code does the rest. excel vba仅实例化我的类对象,而托管代码则完成其余的工作。

The solution is to delete all the .exd files from your %APPDATA% and %TEMP% because... 解决方案是从%APPDATA%和%TEMP%中删除所有.exd文件,因为...

This issue occurs because some scriptable controls are made obsolete in Office 2013 for security reasons. 发生此问题的原因是出于安全原因,某些可编写脚本的控件在Office 2013中已过时。 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. 通过使用仅适用于这些控件的特定于版本的kill-bit来禁用这些可编写脚本的控件,并且仅当在文档中使用它们时才会发生这种情况。 We recommend that you do not try to embed scriptable controls directly into documents, because this behavior may reduce system security 我们建议您不要尝试将可脚本编写的控件直接嵌入文档中,因为这种行为可能会降低系统安全性

Thanks to yay_excel for assisting me. 感谢yay_excel为我提供的帮助。 Here is the page that helped me out: 这是帮助我的页面:

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

Which in turn points to this MSDN Knowledge Base article: MSDN 依次指向此MSDN知识库文章: MSDN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法设置Range类C#Excel Interop的Hidden属性 - Unable to set the Hidden property of the Range class c# excel interop "Unable to set the FreezePanes property of the Window class": Microsoft.Office.Interop.Excel.Window.set_FreezePanes not working in Windows 10 - "Unable to set the FreezePanes property of the Window class" : Microsoft.Office.Interop.Excel.Window.set_FreezePanes not working in Windows 10 获取Excel异常“无法设置范围类的NumberFormat属性” - Get an Excel exception “Unable to set the NumberFormat property of the Range Class” 释放Interop Excel对象 - Release Interop Excel object 如何从从Microsoft.Office.Interop.Excel.Range对象的“value”属性获取的动态类型变量中获取值 - How to get a value from a dynamic type variable obtained from the “value” property of Microsoft.Office.Interop.Excel.Range object 无法将“microsoft.Office.Interop.Excel.ApplicationClass”类型的 COM 对象转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'" Visual Studio Windows 窗体 C# Interop.Excel - Excel 类对象 - Visual Studio Windows Forms C# Interop.Excel - Excel Class Object Excel Interop(Visual Studio C#)-“嵌入式Interop类型”属性 - Excel Interop (Visual Studio C#) - “Embed Interop Type” property 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" 无法转换“Microsoft.Office.Interop.Excel.WorksheetClass”类型的COM对象 - Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM