我试图从我的Python代码调用一个VBA sub来将指定文件夹中的所有excel文件从xls转换为xlsm格式.
当我在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代码:
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)
有谁知道如何成功地将Python变量传递给VBA子?
谢谢!
解决方法:
Per Tim Williams的建议我读了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)
现在代码完美无缺!
标签:python,excel,excel-vba,vba 来源: https://codeday.me/bug/20190623/1268294.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。