views:

47

answers:

2

Here is my code,

    For Each pj In wdApp.Application.VBE.VBProjects
       x = pj.FileName
       y = pj.Protection
        If x <> "" Then
          If y <> "1" Then
             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
                      cboMacros.AddItem (curMacro)
                      Debug.Print curMacro
                   Else: newMacro = Null 
                   End If
                Next
             Next
         End If
         Selection.InsertAfter vbCr
       End If
       x = ""
    Next
    Selection.Collapse wdCollapseEnd
    End Sub

the issue is, I can return the name of all macros in an associated file, but it is horribly inefficient. It takes @2 minutes for 85 macro names. This is because the program reads every line of every module (CountOfLines). I was just hoping there may be some magic that could be performed in my else statement. If the current macro is the same as newmacro then just skip to when they are different. I am not sure if this is possible? If not, is there a better method to use that CountOfLines?

A: 

The code here pulls all the macro names (39) in the file I am working on pretty much instantly. Can you not adapt it instead?

Lunatik
A: 

One problem you have is that ProcKind is an output, not an input, so you first need to create a variable like this:

Dim ProcKind As VBIDE.vbext_ProcKind

Then your statement would be

newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, prockind)

The real answer is that you don't need to go through every line, you can skip from proc to proc:

             For Each vbcomp In pj.VBComponents
                With vbcomp.CodeModule
                myStartLine = .CountOfDeclarationLines + 1
                While myStartLine < .CountOfLines
                  newMacro = .ProcOfLine(i,prockind)
                  numLines = .ProcCountLines(newMacro,vbext_pk_Proc)   
                  If curMacro <> newMacro Then
                      curMacro = newMacro
                      cboMacros.AddItem (curMacro)
                      Debug.Print curMacro
                  Else: newMacro = Null
                  End If
                  myStartLine = myStartLine + numLines
                Wend
                End With
             Next
Lance Roberts