从 Python 执行 MS Access 宏时出错 [英] Error executing a MS Access macro from Python

查看:55
本文介绍了从 Python 执行 MS Access 宏时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Python 运行宏.当从 Access 本身执行时,所述宏工作正常,但我正在尝试使用以下 Python 代码(从 如何从 python 运行 MS Access 宏):

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

不幸的是,执行倒数第二行 (...RunMacro("my_macro")) 会导致以下不透明的错误消息:

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)

知道这意味着什么吗?

推荐答案

如果您将宏命名为AutoExec",它将在您打开数据库时自动运行.

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

来源:创建一个在打开数据库时运行的宏

AutoExec 宏就是名为 AutoExec 的宏.当一个数据库启动,Access 在运行任何宏之前运行 AutoExec 宏其他宏或 VBA 代码.

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.

这篇关于从 Python 执行 MS Access 宏时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆