如何从python运行MS Access宏

时间:2012-02-07 14:26:27

标签: python macros ms-access-2007

我正在尝试从python脚本运行现有的Microsoft Access宏。 我现在拥有的是

import win32api,time
from win32com.client import Dispatch

strDbName = 'Exit.mdb'
objAccess = Dispatch("Access.Application")
objAccess.Visible = False
objAccess.OpenCurrentDatabase(strDbName)
objDB = objAccess.CurrentDb()
objAccess.run('test')
objAccess.Application.Quit()

当我运行此操作时,我收到错误

Traceback (most recent call last):
  File "accessmacro.py", line 10, in <module>  
    objAccess.run('test')  
  File "<COMObject Access.Application>", line 2, in run  
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, u"Microsoft    Office  Access can't find the procedure 'test.'", None, -1, -2146825771), None)

1 个答案:

答案 0 :(得分:5)

Run Method需要“用户定义的函数或子过程”的名称。但是,如果'test'是宏的名称,请使用RunMacro Method

objAccess.DoCmd.RunMacro('test')