用於列出和列印工作簿中所有巨集的名稱和程式碼的巨集/VBA 程式碼

用於列出和列印工作簿中所有巨集的名稱和程式碼的巨集/VBA 程式碼

我正在使用 Excel 2007 對於這個問題,我的工作簿的名稱是 PrintCode.xlsm

是否有巨集或 VBA 程式碼可以列印開啟的工作簿中的所有巨集名稱和程式碼?

我在網上找到了一些示例,但似乎都不起作用?

答案1

我找到了這個,看看是否是你需要的:如何使用 Visual Basic 6.0 從 Excel 工作簿中擷取巨集的名稱

為按鈕定義點選事件處理程序。在此過程中使用以下程式碼來顯示有關 C:\Abc.xls 中定義的巨集的資訊:

Private Sub Command1_Click()
    ' Declare variables to access the Excel workbook.
    Dim objXLApp As Excel.Application
    Dim objXLWorkbooks As Excel.Workbooks
    Dim objXLABC As Excel.Workbook

    ' Declare variables to access the macros in the workbook.
    Dim objProject As VBIDE.VBProject
    Dim objComponent As VBIDE.VBComponent
    Dim objCode As VBIDE.CodeModule

    ' Declare other miscellaneous variables.
    Dim iLine As Integer
    Dim sProcName As String
    Dim pk As vbext_ProcKind

    ' Open Excel, and open the workbook.
    Set objXLApp = New Excel.Application
    Set objXLWorkbooks = objXLApp.Workbooks    
    Set objXLABC = objXLWorkbooks.Open("C:\ABC.XLS")

    ' Empty the list box.
    List1.Clear

    ' Get the project details in the workbook.
    Set objProject = objXLABC.VBProject

    ' Iterate through each component in the project.
    For Each objComponent In objProject.VBComponents

        ' Find the code module for the project.
        Set objCode = objComponent.CodeModule

        ' Scan through the code module, looking for procedures.
        iLine = 1
        Do While iLine < objCode.CountOfLines
            sProcName = objCode.ProcOfLine(iLine, pk)
            If sProcName <> "" Then
                ' Found a procedure. Display its details, and then skip 
                ' to the end of the procedure.
                List1.AddItem objComponent.Name & vbTab & sProcName
                iLine = iLine + objCode.ProcCountLines(sProcName, pk)
            Else
                ' This line has no procedure, so go to the next line.
                iLine = iLine + 1
            End If
        Loop
        Set objCode = Nothing
        Set objComponent = Nothing
    Next

    Set objProject = Nothing

    ' Clean up and exit.
    objXLABC.Close
    objXLApp.Quit
End Sub

相關內容