How to call Excel VBA functions and subs using Python win32com?

对着背影说爱祢 提交于 2019-12-18 01:05:13

问题


My Excel workbook contains VBA subs and macros similar to those below; they sit in Module1.

How to call them using Python win32com module?

Public Sub setA1(ByVal s As String)
    ThisWorkbook.ActiveSheet.Range("A1").Value = s
End Sub

Public Function getA1() As String
    getA1 = ThisWorkbook.ActiveSheet.Range("A1").Value
End Function

Many thanks in advance!


回答1:


import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="c:\\temp\\book1.xls",ReadOnly=1)
xl.Application.Run("setA1", '4')
res = xl.Application.Run("getA1")
print res
xl = 0

Just as simple as this ....



来源:https://stackoverflow.com/questions/14732340/how-to-call-excel-vba-functions-and-subs-using-python-win32com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!