0

I'm trying to run a macro from Python. Said macro works fine when executed from Access itself, but I'm trying to do it with the following Python code (handily stolen from How to run a MS Access macro from python):

from win32com.client import Dispatch

access_obj = Dispatch("Access.Application")
access_obj.Visible = False
access_obj.OpenCurrentDatabase("my_database_file.accdb")
access_obj.DoCmd.RunMacro("my_macro")
access_obj.DoCmd.CloseDatabase()

Executing the penultimate line (...RunMacro("my_macro")) unfortunately results in the following opaque error message:

com_error: (-2147352567, 'Exception occurred.', (0, None, 'You canceled the previous operation.', 'vbaac10.chm', 5738, -2146826287), None)

Any idea what this even means?

acdr
  • 4,538
  • 2
  • 19
  • 45
  • If you test again with `access_obj.Visible = True`, does anything in the Access application window provide details about the error situation? – HansUp Apr 23 '19 at 13:13
  • 1
    @HansUp: that... seems to have worked. And now with `Visible` set to `False` it works as well, despite me not having changed anything. I'll leave the question up though, because your suggestion is a good one. – acdr Apr 23 '19 at 13:29

1 Answers1

0

If you name the macro "AutoExec" it will run automatically when you open the database.

Source: Create a macro that runs when you open a database

An AutoExec macro is just a macro that is named AutoExec. When a database starts, Access runs the AutoExec macro before it runs any other macros or VBA code.

smoore4
  • 4,520
  • 3
  • 36
  • 55