Python的$ C $下运行MS-接入模块子程序 [英] Python code for running ms-Access Module Subroutine

查看:441
本文介绍了Python的$ C $下运行MS-接入模块子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的节目,这是我对计算器的第一个问题。我试图使Python打开.ACCDB文件并运行它已经在Access中定义的子程序。我管理使用这个code与Excel做到这一点:

I am very new to programming and this is my first question on stackoverflow. I am trying to make python open an .accdb file and run a subroutine which is already defined in Access. I manage to do it with Excel using this code:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Visible=True
xl.Workbooks.Open(Filename="<mydirectory>\\open",ReadOnly=1)
xl.Application.Run("TestMe")
#...access spreadsheet data...
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl=0

分TESTME看起来是这样的:

The Sub TestMe looks like this:

Sub TestMe()
MsgBox "Hi there"
End Sub

运行Python的code promtly推出的Excel,打开文件open.xlsm并显示一个消息框。到目前为止,一切都很好。感谢:<一href="http://stackoverflow.com/questions/345920/need-skeleton-$c$c-to-call-excel-vba-from-pythonwin#new-answer">Need骨架code调用Excel的VBA从PythonWin的

我已经修改了code,试图与Access实现同样的。我做了一个名为testdb的新.ACCDB文件,并复制上面的子程序TESTME变成了VBA模块。修改后的蟒蛇code是这样的:

I've modified the code to try to acheive the same with Access. I made a new .accdb file called "testdb" and copied the above subroutine "TestMe" into a VBA module. The modified python code looks like this:

import win32com.client
xl=win32com.client.Dispatch("Access.Application")
xl.Visible=True
xl.OpenCurrentDatabase("<mydirectory>\\testdb.accdb")
xl.Application.Run("TestMe")
#...access spreadsheet data...
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl=0

主要的变化是,Workbooks.Open已变为OpenCurrentDatabase方法。我第一次尝试找到一些比较类似,如Databases.Open,但没有运气。运行新的code启动访问和打开文件testdb.accdb,但仅此而已,没有出现消息框。唯一的控制台输出我能想象的任何感兴趣的是:

The main change is that "Workbooks.Open" has changed to "OpenCurrentDatabase". I first tried to find something more similar, like "Databases.Open", but with no luck. Running the new code launches Access and opens the file testdb.accdb, but that's it, no messagebox appears. The only Console output I can imagine is of any interest is:

xl.Application.Run("TestMe")
File "<COMObject <unknown>>", line 14, in Run

result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,        argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352562), None)

我很茫然。任何帮助将大大AP preciated!

I am quite at a loss. Any help would be greatly appreciated!

推荐答案

考虑创建一个运行code 行动调用的函数一个新的Access宏对象该模块。然后,调用使用使用 DoCmd.RunMacro 方法。

Consider creating a new Access macro object with a RunCode action that calls the function in the module. Then, call the macro in Python's Windows COM API using use the DoCmd.RunMacro method.

MACRO

Macro
RunCode: TestMe()

注:呼叫:只有函数可以用运行code 不是子程序,除非你创建一个VBA模块功能调用子程序引用SubroutineName

NOTE: Only functions can be referenced with RunCode not subroutines unless you create a VBA module function that calls the subroutine: Call SubroutineName:

的Python

import win32com.client
ac = win32com.client.Dispatch("Access.Application")
ac.Visible=True
ac.OpenCurrentDatabase("<mydirectory>\\testdb.accdb")
ac.DoCmd.RunMacro('MacroName')

ac.DoCmd.CloseDatabase
ac = None

这篇关于Python的$ C $下运行MS-接入模块子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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