howto click button using .net

  • Thread starter Abraham Andres Luna
  • Start date
A

Abraham Andres Luna

hello everyone,
how do i click a commandbutton using .net and the Excel 11.0 Object Library

this is my code, but the error says it cannot cast system.__comobject to the
excel.oleobjectclass

Microsoft.Office.Interop.Excel.Application appExcel = new
Microsoft.Office.Interop.Excel.Application();
appExcel.Workbooks.Open(@"C:\RDK\Test.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet wsSetup =
(Microsoft.Office.Interop.Excel.Worksheet)appExcel.Sheets["Sheet1"];
Microsoft.Office.Interop.Excel.OLEObjectClass objRefresh =
(Microsoft.Office.Interop.Excel.OLEObjectClass)wsSetup.OLEObjects("CommandButton1");
//After casting it i guess i want to call the click method
//objRefresh.Click() or something
string strType = objRefresh.OLEType.ToString();
appExcel.ActiveWorkbook.Close(false, Type.Missing, Type.Missing);
appExcel.Quit();
MessageBox.Show(strType);

thank you for your help
 
A

Abraham Andres Luna

i found some code that used msforms to cast the object
so i changed my code to:


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

appExcel.Workbooks.Open(@"C:\RDK\Test.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

Microsoft.Office.Interop.Excel.Worksheet wsSetup =
(Microsoft.Office.Interop.Excel.Worksheet)appExcel.Sheets["Sheet1"];

CommandButtonClass cbRefresh =
(CommandButtonClass)wsSetup.OLEObjects("CommandButton1"); //this line fails

//Microsoft.Office.Interop.Excel.OLEObjectClass objRefresh =
(Microsoft.Office.Interop.Excel.OLEObjectClass)wsSetup.OLEObjects("CommandButton1");

string strType = cbRefresh.Name;

appExcel.ActiveWorkbook.Close(false, Type.Missing, Type.Missing);

appExcel.Quit();

MessageBox.Show(strType);



thank you for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top