9

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!

Yulia V
  • 3,507
  • 10
  • 31
  • 64
  • 1
    This might help you : http://stackoverflow.com/questions/345920/need-skeleton-code-to-call-excel-vba-from-pythonwin – Eric Feb 06 '13 at 15:27

1 Answers1

15
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 ....

Yulia V
  • 3,507
  • 10
  • 31
  • 64