WD2000: Sample Macro to Return Macro and Procedure Names (262961)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q262961

SUMMARY

This article provides a sample macro that you can use to list the names of macros that are contained in active projects in Word 2000.

The ListAllMacroNames sample macro lists the names of macros in all projects that are not locked for viewing. For projects that are locked, the sample macro displays a "Macros in this locked project will not be listed" message, and it does not list the macros names. If you want to view a list of macros, point to Macro on the Tools menu, and then click Macros.

NOTE: When you load an add-in program, projects that are contained in it are locked. Therefore, the ListAllMacroNames sample macro does not list macros in add-in programs if the projects are locked.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

NOTE: For the following macro to run, you must add the Visual Basic for Applications (VBA) Extensibility reference. To do this, follow these steps:
  1. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  2. In the Visual Basic Editor, click References on the Tools menu.
  3. In the Available References list, click to select the Microsoft Visual Basic for Applications Extensibility check box.
  4. Click OK to close the References dialog box.
To list the macro names stored in the public projects, create the following VBA macro:
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
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbmacroexample KB262961