【发布时间】:2021-01-08 09:37:30
【问题描述】:
我正在尝试在 Excel 工作表上添加一个按钮。 根据来自互联网的示例,我正在尝试执行以下代码。
using Excel = Microsoft.Office.Interop.Excel;
using VBIDE = Microsoft.Vbe.Interop;
private static void excelAddButtonWithVBA()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlBook = xlApp.Workbooks.Open(@"PATH_TO_EXCEL_FILE");
Excel.Worksheet wrkSheet = xlBook.Worksheets[1];
Excel.Range range;
try
{
//set range for insert cell
range = wrkSheet.get_Range("A1:A1");
//insert the dropdown into the cell
Excel.Buttons xlButtons = wrkSheet.Buttons();
Excel.Button xlButton = xlButtons.Add((double)range.Left, (double)range.Top, (double)range.Width, (double)range.Height);
//set the name of the new button
xlButton.Name = "btnDoSomething";
xlButton.Text = "Click me!";
xlButton.OnAction = "btnDoSomething_Click";
buttonMacro(xlButton.Name, xlApp, xlBook, wrkSheet);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
xlApp.Visible = true;
}
但它一直说“Excel不包含按钮”
使用 Button 属性应该包含哪些参考?
【问题讨论】:
-
wrkSheet.Buttons();你可以试试 wrkSheet.Buttons; ?
-
工作表没有按钮功能。要使用工作表按钮,我需要包含哪些参考?
标签: c# excel excel-interop