简体   繁体   中英

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. I am using Office 2013 (32-bit).

I have written a C# class library that is COM exposed. The excel workbook instantiates an object and passes in a reference to the current workbook. The object then calls the excel object and updates Label1's caption on the first sheet.

I have opened up all trust center settings (it works with .xls files) and the .xlsm file is not protected in anyway.

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 .

//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.

The solution is to delete all the .exd files from your %APPDATA% and %TEMP% because...

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

Thanks to yay_excel for assisting me. 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

Which in turn points to this MSDN Knowledge Base article: MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM