从Python执行MS Access宏时出错

2024-09-27 23:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Python运行宏。当从Access本身执行时,所说的宏工作得很好,但我尝试使用以下Python代码(很容易从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"))会导致以下不透明错误消息:

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

你知道这是什么意思吗?你知道吗


Tags: torun代码fromnoneobjaccessmy
1条回答
网友
1楼 · 发布于 2024-09-27 23:22:42

如果将宏命名为“AutoExec”,则当您打开数据库时,它将自动运行。你知道吗

资料来源: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.

相关问题 更多 >

    热门问题