【问题标题】:How to call Excel VBA functions and subs using Python win32com?如何使用 Python win32com 调用 Excel VBA 函数和子函数?
【发布时间】:2013-01-21 20:15:42
【问题描述】:

我的 Excel 工作簿包含类似于以下的 VBA 子程序和宏;他们坐在Module1中。

如何使用 Python win32com 模块调用它们?

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

提前非常感谢!

【问题讨论】:

标签: python excel vba win32com


【解决方案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

就这么简单......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多