0

I am trying to automate some tasks in MS Access which are involving some button presses. Here are my best guesses so far, but I never find a way to finally execute a click...

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?

Or maybe there is another approach using the Microsoft.Office.Interop.Access.CommandButton instance?

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);

Is this possible using the MS Office Interop assemblies? Or are there any alternatives? Unfortunately, these interop assemblies are very poorly documented.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
  • What does the button do when clicked? If it's calling a sub for example, why not call the sub directly? – Kostas K. Oct 14 '22 at 15:27
  • @KostasK. I need a generic solution which works for any button. But yeah, I also tried to programatically retrieve the name of the OnClick-Sub of the button and execute it directly, but I also didn't find a solution for this approach. Any idea how to do this? – ˈvɔlə Oct 14 '22 at 16:59
  • 1
    You cannot get the button's click handler cause it's private to the form. The sub/function must be declared public inside a standard module. See this, it's in VBA but you can easily convert it to C#. https://learn.microsoft.com/en-us/office/vba/api/access.application.run – Kostas K. Oct 15 '22 at 08:20
  • @KostasK. So it is not possible to execute a button click via C#? – ˈvɔlə Oct 15 '22 at 08:48
  • Just calling a public sub if you know it's name? – ˈvɔlə Oct 15 '22 at 09:10
  • I'm not aware of a way to click a button programmatically, since it's user input. SendKeys maybe, but I'm not very familiar with it (not sure if it's even possible in C#). Public sub/function in a standard module is the only way I know of. – Kostas K. Oct 15 '22 at 10:13

1 Answers1

0

You can use the CommandBars.ExecuteMso method which executes the control identified by the idMso parameter. This method is useful in cases where there is no object model for a particular command. Works on controls that are built-in buttons, toggleButtons, and splitButtons. On failure it returns E_InvalidArg for an invalid idMso, and E_Fail for controls that are not enabled or not visible. For example:

Application.CommandBars.ExecuteMso("Copy")

If you need to execute non-ribbon controls you may consider using Windows API functions or Accessibility API for that.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45