返回所有宏的列表

时间:2010-10-13 10:38:30

标签: vba outlook ms-word

在vba中,如何调用word文档并获取其中包含的所有宏的列表?

1 个答案:

答案 0 :(得分:0)

本文应该向您展示如何操作(它适用于Word 2000,但我认为它很有可能适用于您使用的任何版本):WD2000: Sample Macro to Return Macro and Procedure Names

这是该文章的代码:

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