Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
113 个问题
我收到有关自动运行Excel宏程序的要求,但我只熟悉Windows Forms App。我尝试过Microsoft.Office.Interop.Excel来激活按钮。它不起作用。
public bool openFileExcel(string filename)
{
Excel.Application _xlsm = new Excel.Application();
Excel.Workbook _xlsB = _xlsm.Workbooks.Open(_fPath + filename);
_xlsm.Visible = true;
Excel.Worksheet _xlsS = _xlsB.Worksheets["Control"];
Excel.OLEObject _oObj = _xlsS.OLEObjects("m_btnReset");
_xlsm.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
if (_xlsm.Ready)
{
try
{
_oObj.Select();
_oObj.Activate();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return false;
}
else
{
return false;
}
}
请指导我。
Note:此问题总结整理于: Problem trigger Excel ActiveX button from another Windows Forms App
根据我的测试,您可以尝试使用属性_oObj.Object.Value = true ;
从Winform应用程序触发excel activex按钮。
下面是一个可以参考的代码示例。
private void button1_Click(object sender, EventArgs e)
{
string xlFileName = Path.Combine("D:\\", "test1.xlsm");
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Open(xlFileName);
var xlSheet = xlWorkbook.Worksheets["Sheet1"] as Excel.Worksheet;
Excel.OLEObject _oObj = xlSheet.OLEObjects("CommandButton1");
_oObj.Object.Value = true ; // This code will trigger the button
xlWorkbook.Save();
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null;
Marshal.ReleaseComObject(xlApp); xlApp = null;
}
如果回复有帮助,请点击“接受答案”并点赞。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。