tags:

views:

30

answers:

1

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

Thanks

A: 

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
ho1
the vba editor complains, even at the first line of code. I hate this stuff tbh :D
tom
@tom: Did you add the reference to `Microsoft Visual Basic for Applications Extensibility` as mentioned on that link? I only copied the code into my answer, not the other steps.
ho1
yes i already have that setup.
tom
may i ask, if im using thsis in outlook, how would i ammend the code to use a word file. i know how to get the word object etc but am unsure how to modify the code.
tom
@tom: Don't know I'm afraid. I'm not that experienced with pure Office development.
ho1
well it works now but the thing is so dam slow, is that natural between office applications?
tom
@tom: Everything is relative, but I'm used to things being a bit slow. Assuming that it has to open up Word and the load the document before it can start it's work, I'd assume it'll be fairly slow.
ho1