我绝不是 Python 方面的专家,但对 Access 和 Excel VBA 相当熟悉。如果我在业余编程生涯中更早地了解 Python,我将永远不会尝试使用任何 VBA 代码来完成我下面打算做的事情。考虑到对 VBA 代码的时间投入,我必须找到一种方法......
在过去的几天/一周里,我一直在试图找到一种使用 Python 的方法->访问 VBA 并满足以下要求:
- 从 Python 调用 Access VBA 函数
- 从 Python 向 Access VBA 函数发送参数
- 从 Access VBA 函数返回值到 Python
我使用 pythonnet 和 IronPython 尝试使用 clr,发现越来越多的混乱和不清楚的错误消息和异常。我尝试了上面建议的 DoCmd.RunMacro 方法,但 Access 宏不返回值。尝试了一个名为数据宏的新(ish)版本的 Access 宏(2010),它确实有一个名为 SetReturnVar 的操作,但除非通过更传统的宏并且如上所述,传统宏不返回值,否则它们不会与 VBA 对话。今天我更仔细地阅读了一些微软文档(Access Application.Run 方法):
Access Application.Run method
我并不完全了解“您不能从 Microsoft Access 以外的任何应用程序设置对单个 Microsoft Access 数据库的引用”这句话的含义,但我发现很多 Python Office 应用程序文章在谈论 PythonExcel VBA 时似乎更成功。我的理由是,因为我过去能够运行 Excel VBA Access VBA,如果 Python Excel VBA 和我读过的一样工作,那么一个解决方案(尽管很复杂)似乎是可能的(我认为正确的程序员称之为 hack)。
在剪切/粘贴代码 sn-ps 和调试大约 1 1/2 小时后:
Python
from win32com.client import Dispatch
FILELOC = r'C:\Users\Desktop\PyExcel.xlsm'
PROGNAME='Excel.Application'
num = 4
#open excel workbook containing VBA code
#...could do more to ensure excel isn't already running
xl = Dispatch(PROGNAME)
xl.Visible = True
#open excel file containing the VBA code
#...could do more to check if file is already open, etc
xl.Workbooks.Open(Filename=FILELOC)
#call to VBA code within excel
rtrn_int = xl.Run("RunCOMObject", num)
#print return value
print(rtrn_int)
#Quit excel-this doesn't work very well and there are articles about
#Python or the COM object not being able to actually remove Excel
#from the task manager
xl.Quit()
Excel VBA
Option Explicit
Private Const ACCESS_FILELOC As String = "C:\Users\Desktop\Test.accdb"
Private Const TEMP_FILELOC As String = "C:\Users\Desktop\TestTemp.accdb"
Function RunCOMObject(intNum As Integer) As Integer
Dim objAcc As Object, objProject As Object
Dim accAppl As Access.Application
Dim MyAppl As String
MyAppl = "Access.Application"
If Not IsRunning(MyAppl) Then 'Access not running, simply start
'up Access and open file
Set accAppl = CreateObject(MyAppl) 'start Access
accAppl.Visible = True
accAppl.OpenCurrentDatabase (ACCESS_FILELOC) 'open file
Else: 'Access is running
On Error Resume Next
Set accAppl = GetObject(, MyAppl) 'assign the running application
'to a variable
On Error GoTo Err_File_Open 'use an error in attempting to rename
'the database of interest to determine if the open file is the
'desired file
Name ACCESS_FILELOC As TEMP_FILELOC 'rename the file of interest
Name TEMP_FILELOC As ACCESS_FILELOC 'file was successfully renamed
'therefore not open
Call NoFileOrOther(accAppl, MyAppl)
End If
Err_File_Open:
'Required Access file is open
RunCOMObject = accAppl.Run("TestLink", intNum) 'run the VBA function in
'Access
accAppl.CloseCurrentDatabase 'close database
accAppl.Quit 'quit Access
Set accAppl = Nothing
End Function
Function IsRunning(ByVal MyAppl As String) As Boolean
Dim applRef As Object
On Error Resume Next 'error occurs if GetObject is unable to find a
'running version of the application
Set applRef = GetObject(, MyAppl) 'attempt to obtain the required
'application object
If Not applRef Is Nothing Then 'if application is already running
Set applRef = Nothing
IsRunning = True
Else 'application is not running
IsRunning = False
End If
Set applRef = Nothing
End Function
Sub NoFileOrOther(accAppl As Access.Application, MyAppl As String)
On Error GoTo Err_No_FileOpen
If accAppl.CurrentProject.Name <> "" Then 'Access active with another a
'different file open
Set accAppl = CreateObject(MyAppl) 'start a new instance of Access
accAppl.Visible = True
accAppl.OpenCurrentDatabase (ACCESS_FILELOC) 'open file
End If
Exit Sub
Err_No_FileOpen:
accAppl.OpenCurrentDatabase (ACCESS_FILELOC) 'in the event of Access
'being active without a database open
End Sub
访问 VBA
这是一个完全微不足道的示例,掩盖了我已经在 Access VBA 中编写的代码量来保证这种解决方法,但它用于演示方法
Option Compare Database
Option Explicit
Function TestLink(intNum As Integer) As Integer
TestLink = intNum + 10
End Function
Python 输出:
14
成功!!!!该数字在 Python 中最初为 4,并作为参数发送到 Excel 和 Access,其中添加了 10,然后通过 Excel 返回到 Python print(rtrn_int) = 14。
如果有人知道如何在不使用 Excel VBA 作为中介的情况下从 Python -> 访问 VBA 并返回值给 Python 明确(即通过上面演示的类似严格)发送参数,我将很高兴收到您的来信。或者使用 pythonnet 引用 clr 的方法将同样受到赞赏。