2016-07-15 60 views
2

我想从我的Python代码中调用一个VBA sub来将指定文件夹中的所有excel文件从xls转换为xlsm格式。如何将变量从Python传递到VBA Sub

我可以使用下面的代码,当我没有在VBA中使用变量,它运作良好。

Python代码:

import os 
import win32com.client 

xl=win32com.client.Dispatch("Excel.Application") 
xl.Workbooks.Open(Filename="C:\Users\Name\Documents\PERSONAL.XLSB", ReadOnly=1) 
xl.Application.Run("PERSONAL.XLSB!Module1.xlstoxlsmFinal" 
xl.Application.Quit() # Comment this out if your excel script closes 
del xl 

VBA代码:

Public Sub xlstoxlsmFinal() 

' goes through all the sub folders of a specified folder and created an xlsm(macro enabled version) of any xls documents 


    Dim fso, oFolder, oSubfolder, oFile, queue As Collection 

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set queue = New Collection 
    Path = "C:\Users\Name\Documents\Monthly Reports\16.06 Reports\Agent Reports" 
    queue.Add fso.GetFolder(Path) 

    Do While queue.Count > 0 
     Set oFolder = queue(1) 
     queue.Remove 1 'dequeue 
     '...insert any folder processing code here... 
     For Each oSubfolder In oFolder.SubFolders 
      queue.Add oSubfolder 'enqueue 
     Next oSubfolder 
     For Each oFile In oFolder.Files 
       If Right(oFile, 4) <> "xlsm" And Right(oFile, 3) <> "pdf" Then 
       Workbooks.Open Filename:=oFile 
       ActiveWorkbook.SaveAs Filename:=oFile & "m", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False 
       ActiveWorkbook.Close 
    End If 
     Next oFile 
    Loop 

但是,我无法调用函数时,我尝试从蟒蛇传递变量到VBA。以下是我迄今为止所尝试的内容。

Python代码与变量:

import os 
import win32com.client 

Datev = """16.06 """ 
xl=win32com.client.Dispatch("Excel.Application") 
xl.Workbooks.Open(Filename="C:\Users\Name\Documents\PERSONAL.XLSB", ReadOnly=1) 
xl.Application.Run("PERSONAL.XLSB!Module1.xlstoxlsmFinal(" + Datev + ")") 
## xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function. 
xl.Application.Quit() # Comment this out if your excel script closes 
del xl 

VBA代码变量:

Public Sub xlstoxlsmFinal(Datev As String) 

' goes through all the sub folders of a specified folder and created an xlsm(macro enabled version) of any xls documents 


    Dim fso, oFolder, oSubfolder, oFile, queue As Collection 

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set queue = New Collection 
    Path = "C:\Users\Name\Documents\Monthly Reports\" & Datev & "Reports\Agent Reports" 
    queue.Add fso.GetFolder(Path) 

    Do While queue.Count > 0 
     Set oFolder = queue(1) 
     queue.Remove 1 'dequeue 
     '...insert any folder processing code here... 
     For Each oSubfolder In oFolder.SubFolders 
      queue.Add oSubfolder 'enqueue 
     Next oSubfolder 
     For Each oFile In oFolder.Files 
       If Right(oFile, 4) <> "xlsm" And Right(oFile, 3) <> "pdf" Then 
       Workbooks.Open Filename:=oFile 
       ActiveWorkbook.SaveAs Filename:=oFile & "m", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False 
       ActiveWorkbook.Close 
    End If 
     Next oFile 
    Loop 

当我在python运行此我得到的错误信息:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Cannot run the macro 'PERSONAL.XLSB!Module1.xlstoxlsmFinal(16.06)'. The macro may not be available in this workbook or all macros may be disabled.", u'xlmain11.chm', 0, -2146827284), None) 

会不会ody知道如何成功地将Python变量传递给VBA子?

谢谢!

+0

http://www.rondebruin.nl/win/s9/win001.htm请参阅该页末尾的“返回值...”部分。 –

+0

谢谢,这是exaclty我需要 – jvk777

回答

2

每蒂姆·威廉姆斯建议我读rondebruin.nl/win/s9/win001.htm的最后一节,制定了Python代码

import os 
    import win32com.client 

    Datev = """16.06 """ 
    xl=win32com.client.Dispatch("Excel.Application") 
    xl.Workbooks.Open(Filename="C:\Users\Name\Documents\PERSONAL.XLSB", ReadOnly=1) 
    xl.Application.Run("PERSONAL.XLSB!Module1.xlstoxlsmFinal", Datev) 
    xl.Application.Quit() # Comment this out if your excel script closes 
    del xl 

的材料差异改变行:

xl.Application.Run("PERSONAL.XLSB!Module1.xlstoxlsmFinal(" + Datev + ")") 

到:

xl.Application.Run("PERSONAL.XLSB!Module1.xlstoxlsmFinal", Datev) 

现在代码完美地工作!

1

转到开发商标签下的宏安全性,并选择启用所有宏选项

+0

谢谢,但启用所有的宏已被选中 – jvk777

+0

我不知道这是否有助于在这种特殊情况下,但你试过[xlwings](http://xlwings.org/)? –

+0

我在Google上搜索时看到了一些引用,也许我会试一试 – jvk777