2016-07-15 107 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 

しかし、私は私のpythonから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コード:

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) 

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でこれを実行すると、私はエラーメッセージが表示されます

anyb odyは、Python変数をVBAサブに継承する方法を知っていますか?

ありがとうございます!

+0

http://www.rondebruin.nl/win/s9/win001.htmそのページの最後にある「戻り値...」セクションを参照してください。 –

+0

ありがとう、thats exaclty私が必要なもの – jvk777

答えて

2

毎のティム・ウィリアムズの提案は、私がrondebruin.nl/win/s9/win001.htmの最後のセクションを読んですべてのマクロオプションを有効にして、Pythonのコードを策定

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

に:

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) 

コードは完全に機能します。

1

デベロッパータグの下のマクロのセキュリティに移動し、選択し

+0

ありがとう、すべてのマクロが有効になっている – jvk777

+0

この特定の状況で役立つかどうかわかりませんが、[xlwings](http://xlwings.org/)を試しましたか? –

+0

私はグーグルグーグルでそれについていくつかの参照を見た、多分私はそれを試してみましょう – jvk777

関連する問題