问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/55811969/error-executing-a-ms-access-macro-from-python