How to run a MS Access macro from python

余生长醉 提交于 2020-01-02 04:21:04

问题


I'm trying to run an existing Microsoft Access Macro from a python script. What I currently have is

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

When I run this I get the error

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:


The Run Method expects the name of a "user-defined Function or Sub procedure". But, if 'test' is the name of a macro, use RunMacro Method

objAccess.DoCmd.RunMacro('test')


来源:https://stackoverflow.com/questions/9177984/how-to-run-a-ms-access-macro-from-python

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