ggdd2007

Hi,

I have created an Excel .NET project with the VSTO using a previously made Excel Document. I cannot reference any of the controls (command buttons) using the FindControl method.

I get the following exception:
Unable to get the Object property of the OLEObject class

System.Runtime.InteropServices.COMException (0x800A03EC): Unable to get the Object property of the OLEObject class

Source code examples:

Control(string name, Excel.Worksheet sheet ){
Excel.OLEObject obj = (Excel.OLEObject) sheet.OLEObjects(name);
return obj.Object;
}


Your help is greatly appreicated,
Nick



Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Cindy Meister

Which version of Visual Studio did you use to create the project And which are you now using Which version(s) of the .NET Framework are installed on the machine where you're seeing the error message






Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

The version of Visual Studio is 2005.

Th .NET Framework is 2.0.

Thanks

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Cindy Meister

ggdd2007 wrote:

I have created an Excel .NET project with the VSTO using a previously made Excel Document. I cannot reference any of the controls (command buttons) using the FindControl method.

I get the following exception:
Unable to get the Object property of the OLEObject class

System.Runtime.InteropServices.COMException (0x800A03EC): Unable to get the Object property of the OLEObject class

Source code examples:

Control(string name, Excel.Worksheet sheet ){
Excel.OLEObject obj = (Excel.OLEObject) sheet.OLEObjects(name);
return obj.Object;
}

The version of Visual Studio is 2005.

Th .NET Framework is 2.0.

OK, I needed to be sure you weren't trying to work with the old VS 2003. The method FindControl was designed for the previous version of VSTO, but it's not supported in the newer version. VSTO 2005 uses WindowsForms controls, rather than ActiveX objects as its "native" embedded controls.

Excel itself still supports ActiveX controls, but in order to get to and use the OLE Server represented by the Object property it's first necessary to "activate" the object. For this, you get finest control using the DoVerb method

Where are you seeing these code examples that have set you on this path Any reason these are being used instead of WindowsForms controls






Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

Thank you for your kindness.

Because I upgraded my solution from VS2003 to VS2005, I use this source code.

I don' t know how to use "Verb" method.

And I have not sought a sample C# code to use it.

Please tell me how to use it.

Excel.OLEObject obj = (Excel.OLEObject)sheet.OLEObjects(name);

obj.Verb() ←?????

Thanks

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Cindy Meister

Hi Nick

Do a Google Groups search on the office.developer set of newsgroups (see the "Please Read First" message for the links) on the terms: ActiveX C# Excel. This has been discussed there, with code samples sometime during the last year or so.

But you might be faster to delete the ActiveX buttons and use WindowsForms buttons in their place...






Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

Thank you for your kindness.

I am searching the office developer set of newsgroups.

But I don' t still find it.

Please tell me "url links".

Thanks,

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Ji Zhou – MSFT

Hi Nick,

I think OLEObjects() function needs an Int parameter to be index. Codes I write as follows work fine in my side.

Code Snippet
object missing = Type.Missing;

Excel.Application myApp = new Microsoft.Office.Interop.Excel.Application();

myApp.Visible = true;

myApp.Workbooks.Open(@"C:\book1.xlsx",missing,missing,missing,missing,missing,

missing,missing,missing,missing,missing,missing,missing,missing,missing);

Excel.Worksheet myWS = myApp.ActiveSheet as Excel.Worksheet;

object name = 1;

Excel.OLEObject control = myWS.OLEObjects(1) as Excel.OLEObject;

MessageBox.Show(control.Name);

So if use OLEObject’s name as parameter, it makes sense to return a null.

Thanks

Ji






Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

Hi Ji,

Thank you for your help!

I tried your sample code, but I did't solve my problem.

Excel.OLEObject control = myWS.OLEobject(1) as Excel.OLEObject;

return control.Object;

I did' t get Object' s property.

Thanks

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

I want to get a OLEobject' property which is a "OLE automation Object".

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Ji Zhou – MSFT

Hi Nick,

Sorry for misunderstanding your question.

As Cindy said, you should try to Active the OLEObject and then get it's object property, like this: (I have only one word OLEObject in my excel file, this works in my side)

Code Snippet

object missing = Type.Missing;

Excel.Application myApp = new Microsoft.Office.Interop.Excel.Application();

myApp.Visible = true;

myApp.Workbooks.Open(@"C:\book1.xlsx", missing, missing, missing, missing, missing,

missing, missing, missing, missing, missing, missing, missing, missing, missing);

Excel.Worksheet myWS = myApp.ActiveSheet as Excel.Worksheet;

Excel.OLEObject control = myWS.OLEObjects(1) as Excel.OLEObject;

control.Activate();

Word.DocumentClass word = control.Object as Word.DocumentClass;

word.Content.Text = "Hello";

Hope this helps!

Thanks

Ji







Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

Thank you for everyone.

I did't still solve it.

Excel.OLEObject control = myWS.OLEObjects(1) as Excel.OLEObject;

control.Activate();

I can't activate this control.

System.Runtime.Interop.Services.COMException(0x800A03EC)

Thanks

Nick





Re: Visual Studio Tools for Office FindControl not locating object on Sheet

Ji Zhou – MSFT

Hi Nick,

It still works fine in my side. Could you please post some more codes to show the context of your code snippet

Thanks

Ji






Re: Visual Studio Tools for Office FindControl not locating object on Sheet

ggdd2007

Thank you for everyone.

I forgot to tell everyone that I am using Managecode Assembly.

I am searching msdn, and I found these articles:

Replacing ActiveX Controls with Managed Controls

If you used ActiveX controls on your document or workbook, you should delete them and replace them with managed controls from the Visual Studio Toolbox. ActiveX controls have limitations in the new Visual Studio Tools for Office projects; for example, you cannot bind data to ActiveX controls, and you must use a special interop assembly to be able to code against them. For this reason, ActiveX controls are not supported for use in new Visual Studio Tools for Office projects.

MSForms.CommandButton btn = (MSForms.CommandButton)FindControl(name);

object FindControl(string name ) {
return FindControl(name, (Excel.Worksheet)thisWorkbook.ActiveSheet);
}

object FindControl(string name, Excel.Worksheet sheet ) {
Excel.OLEObject obj;
try {
obj = (Excel.OLEObject)sheet.OLEObjects(name);
return obj.Object;
}
catch {}
return null;
}

I work fine in development setting in which I use VS2005.

But, when I put in web (using IIS) and download excel's file,

I don' t work.

Then I get that error:You can' t get the oleobjet' object property.

Thanks

Nick