Need skeleton code to call Excel VBA from PythonWin

╄→尐↘猪︶ㄣ 提交于 2019-11-28 09:33:49

I am sorry, I dont know python enough. However, the following should help.

Excel's Application object has a Run method - which takes the name of the macro alongwith arguments to it.

Lets assume that the workbook has a macro named test.


Sub test(ByVal i As Integer)
MsgBox "hello world " & i
End Sub

You can call this using Application.Run "test", 1234

This will call the macro and show the messagebox with "hello world 1234".

OK, I got it! Thanks for the help on the Application.Run method. This info, plus the "Microsoft Excel Visual Basic Reference": http://msdn.microsoft.com/en-us/library/aa209782(office.10).aspx--as recommended by Hammond & Robinson in "Python Programming on Win32"--was what was needed.

Here's the skeleton code:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="<your Excel File>",ReadOnly=1)
xl.Application.Run("<your macro name>")
#...access spreadsheet data...
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl=0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!