return a list of all macros

前端 未结 1 1695
野性不改
野性不改 2021-01-28 07:48

In vba, how can i call a word document and get a list of all the macros it contains?

相关标签:
1条回答
  • 2021-01-28 08:23

    This article should show you how to do it (it's for Word 2000 but I assume there's a good chance it'll work for whatever version you use): WD2000: Sample Macro to Return Macro and Procedure Names

    This is the code from that article:

    Sub ListAllMacroNames()
    
    Dim pj As VBProject
    Dim vbcomp As VBComponent
    Dim curMacro As String, newMacro As String
    Dim x As String
    Dim y As String
    
    On Error Resume Next
    curMacro = ""
    Documents.Add
    
    For Each pj In Application.VBE.VBProjects
       x = pj.FileName
       y = pj.Protection
    
       If x <> "" Then
    
          If y <> "1" Then
             Selection.InsertAfter "The " & Chr(34) & x & Chr(34) & _
                " project contains " & "the following macro names:" & vbCr
             Selection.InsertAfter vbCr
    
             For Each vbcomp In pj.VBComponents
    
                For i = 1 To vbcomp.CodeModule.CountOfLines
                   newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, _
                      prockind:=vbext_pk_Proc)
    
                   If curMacro <> newMacro Then
                      curMacro = newMacro
    
                      If curMacro <> "" And curMacro <> "app_NewDocument" Then
                            Selection.InsertAfter newMacro & vbCr
                            Selection.Collapse wdCollapseEnd
                         End If
                   End If
                Next
             Next
         Else
            Selection.InsertAfter "The project " & Chr(34) & x & Chr(34) & _
               " is locked. " & "Macros in this locked project will not be" _
               & " listed." & vbCr & vbCr
         End If
         Selection.InsertAfter vbCr
       End If
       x = ""
    Next
    Selection.Collapse wdCollapseEnd
    End Sub
    
    0 讨论(0)
提交回复
热议问题