我正在尝试自动化MS Access中的一些任务,这些任务涉及一些按钮按下。以下是我迄今为止最好的猜测,但我从未找到最终执行点击的方法。。。
using Microsoft.Office.Interop.Access;
Application access = new Application();
access.OpenCurrentDatabase("SomeDatabaseName.accdb");
access.DoCmd.OpenForm("SomeFormName", AcFormView.acNormal);
access.DoCmd.GoToControl("SomeButtonName");
access.DoCmd... // I can go to the control, but how do I click it?
或者可能还有另一种方法使用Microsoft.Office.Interop.Access.CommandButton
实例?
var form = access.Forms["SomeFormName"];
foreach (var control in form.Controls)
{
if (control is CommandButton button)
{
// I can get the CommandButton, but how do I execute the click procedure?
string onClick = button.OnClick; // it's just this string: "[Event Procedure]"
}
}
access.CloseCurrentDatabase();
access.Quit(AcQuitOption.acQuitSaveNone);
使用MS Office互操作程序集可以实现这一点吗?或者还有其他选择吗?不幸的是,这些互操作程序集的文档记录非常差。
您可以使用CommandBars.ExecuteMso方法来执行由idMso
参数标识的控件。在没有特定命令的对象模型的情况下,此方法非常有用。适用于内置buttons
、toggleButtons
和splitButtons
的控件。失败时,它会为无效的idMso
返回E_InvalidArg
,为未启用或不可见的控件返回E_Fail
。例如:
Application.CommandBars.ExecuteMso("Copy")
如果需要执行非功能区控件,可以考虑使用Windows API函数或Accessibility API。