Error executing a MS Access macro from Python

若如初见. 提交于 2021-01-29 05:27:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!