2

What I want to do is call a macro from my python code. Here isa sample of the sources :

xl = win32.gencache.EnsureDispatch('Excel.Application')
xl.Visible = 1
xl.Workbooks.Open("C:\\Program Files\\Microsoft Office\\Office14\\XLSTART\\perso.xlsm")
xl.Workbooks.Open(argv[1])
xl.Application.Run('perso.xlsm!' + argv[2])
xl.Application.Run('perso.xlsm!' + argv[2] + '2')
xl.Workbooks.Open(argv[0])
xl.Application.Run('perso.xlsm!aggregate_report_ouverture_appli')
xl.Application.Run('perso.xlsm!macro', 'lol')
xl.Save()
xl.Quit() 

The first two macro are working fine. But the last need a parameter to be set ("lol" in this case). With this try :

xl.Application.Run('perso.xlsm!macro', 'lol')

My macro is call but the parameter is not set. Any idea how to do this or where I can find the "javadoc" of this module (yeah i am from Java world !).

If you need more explanations just le me know.

Thanks you.

Damien.

4

1 回答 1

3

不幸的是,在这种情况下,win32com 关于 excel 的完整文档不存在;您将不得不查看 MS office Excel MSDN 的大部分内容(这还不错,您有时只需要修改代码):

http://msdn.microsoft.com/en-us/library/office/bb149081(v=office.12).aspx

至于您的问题,请考虑使用两个参数的示例宏:

Sub Proc(sParam1 As String, iParam2 As Integer)
        MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub

该宏有两个正在寻找的参数。以下 python 代码将使用参数集调用宏:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Visible = True
Path = "C:\\Program Files\\Microsoft Office\\Office14\\XLSTART\\perso.xlsm"
xl.Workbooks.Open(Filename=Path)
param1 = "Jeremy"
param2 = 3
xl.Application.Run("Proc", param1, param2)

我不确定您的情况下的宏期望什么变量类型,但 'lol' 是,但它在您的示例中作为字符串从 python 发送到宏。希望这可以帮助。

于 2013-05-24T17:25:55.603 回答