howto click button using .net

  • Thread starter Thread starter Abraham Andres Luna
  • Start date 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
 
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
 
Back
Top