Reputation: 65
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.
Upvotes: 3
Views: 13452
Reputation: 5504
Unfortunately in this case, the complete documentation for win32com with regards to excel does not exist; you will have to look at the MS office Excel MSDN for most everything (which isn't too bad you just have to finagle the code sometimes):
http://msdn.microsoft.com/en-us/library/office/bb149081(v=office.12).aspx
As for your question, consider the example macro which takes two parameters:
Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub
The macro has two parameters that it's looking for. The following python code will call the macro using the params set:
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)
I'm not sure what variable type the macro in your case is expecting, but 'lol' is but it is being sent to the macro from python as a string in your example. Hope this helps.
Upvotes: 4