ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   howto click button using .net (https://www.excelbanter.com/excel-programming/352801-howto-click-button-using-net.html)

Abraham Andres Luna

howto click button using .net
 
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();
", 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)wsS etup.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



Abraham Andres Luna

howto click button using .net
 
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();

", 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("CommandBut ton1"); //this line fails

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

string strType = cbRefresh.Name;

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

appExcel.Quit();

MessageBox.Show(strType);



thank you for your help




All times are GMT +1. The time now is 11:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com