I have a problem, I want insert data with a button click method, but this method are created in a Ribbon menu (VSTO).
To understand better this, I want make a query on a database send it to a list of persons, and print on excel what i want see from that persons. For example, i just want see name and birthdate, or name, id and profession.
To do this i need select the Workbook opened when i run the application. So i try this:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Excel.Workbook wb = (Excel.Workbook)Globals.ThisWorkbook.InnerObject;
Excel.Worksheet ws = wb.Sheets["Sheet1"];
ws.Cells[1, 2] = "teste";
}
- using Excel = Microsoft.Office.Tools.Excel;
- using ExcelTools = Microsoft.Office.Tools;
- using Microsoft.Office.Interop.Excel;
But i get this error:
**"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Workbook'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B74CBB86-9C9F-4172-9AE7-3CE4A7BFA5EB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."**
Probably is more easy than i’m seeing, but i’m so tired, and i need finish this…
man try this: