how to list all Excel Macro Names using Python

杀马特。学长 韩版系。学妹 提交于 2020-05-19 02:31:52

问题


I am developing this app that loads an excel sheet and allows users to run macros and tag cells. I ran in to a problem trying to read Macros from the Excel sheet. I am using win32com module for all the excel access. So far it has been good to me. But am struggling trying to create code that will provide me with all the macros. I have the corresponding VBA code that does provide with me the macros but I have so far been unsuccessful trying to translate it in to python. I want to read all the Macros using the Python code. heres the VBA code:

Sub ListMacros()

Const vbext_pk_Proc = 0
Dim VBComp As Object
Dim VBCodeMod As Object
Dim oListsheet As Object
Dim StartLine As Long
Dim ProcName As String
Dim iCount As Integer

Application.ScreenUpdating = False
On Error Resume Next
Set oListsheet = ActiveWorkbook.Worksheets.Add
iCount = 1
oListsheet.Range("A1").Value = "Macro"

For Each VBComp In ThisWorkbook.VBProject.VBComponents
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents(VBComp.Name).CodeModule
With VBCodeMod
StartLine = .CountOfDeclarationLines + 1
Do Until StartLine >= .CountOfLines
oListsheet.[a1].Offset(iCount, 0).Value = _
.ProcOfLine(StartLine, vbext_pk_Proc)
iCount = iCount + 1

StartLine = StartLine + _
.ProcCountLines(.ProcOfLine(StartLine, _
vbext_pk_Proc), vbext_pk_Proc)
Loop
End With
Set VBCodeMod = Nothing
Next VBComp

Application.ScreenUpdating = True
End Sub

I do not have any prior experience in VBA. I am struggling with this. Any suggestions as to how to approach this problem will be very much Appreciated.Thanks

Edit: Heres what I have so far!!

# RunInExcel.py - opens named Excel file fName, returns the sheets, modules
import os, os.path, win32com.client

def run_macro(fName, path=os.getcwd()):
    print(path)
    fName = os.path.join(path, fName)
    print(fName)
    xlApp = win32com.client.Dispatch("Excel.Application")
    fTest = xlApp.Workbooks.Open(fName)
    for i in fTest.VBProject.VBComponents:
        print i.Name 

if __name__ == "__main__":
    run_macro("Testing1.xlsm")

But I need the name of the Macros. Thank you for the help!


回答1:


Unfortunately I don't think you can get the macro names directly using VBA. Instead you scan the code module looking for Sub xxx, etc.

def run_macro(fName, path=os.getcwd()):
    print(path)
    fName = os.path.join(path, fName)
    print(fName)
    xlApp = win32com.client.Dispatch("Excel.Application")
    fTest = xlApp.Workbooks.Open(fName)

    for i in fTest.VBProject.VBComponents:
        print i.Name

        num_lines = i.CodeModule.CountOfLines

        for j in range(1, num_lines+1):

            if 'Sub' in i.CodeModule.Lines(j, 1) and not 'End Sub' in i.CodeModule.Lines(j, 1):
                print i.CodeModule.Lines(j, 1)

    xlApp.Application.Quit()
    del xlApp

if __name__ == "__main__":
    run_macro("Testing1.xlsm")

Note that you need to make sure you exit the script properly or you will leave Excel running (check taskmgr).



来源:https://stackoverflow.com/questions/18589877/how-to-list-all-excel-macro-names-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!