• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1444
  • Last Modified:

VSTO C# list names of macro in Excel

Using C# in a VSTO project. Can't seam to figure out how to list the names of the macros so user can select form them in a list box.

 I thought this would get me close but can't get it figured out
1 Solution
You could try the converting the following code (sorry, but my C# skills are not all that good).  The strMacroList() array gets populated with the macro names which you could set your list to.  Or, you could modify the code to just add each entry to your list as it is found.
Hope that helps!

Private Sub GetMacroList()
Dim strMacroList() As String
Dim c As Long
Dim n As Long
With Application.ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
'would assume you would use
'With this.Application.ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    c = .CountOfDeclarationLines + 1
    Do Until c >= .CountOfLines
        ReDim Preserve strMacroList(n)
        strMacroList(n) = .ProcOfLine(c, 0)
        c = c + .ProcCountLines(.ProcOfLine(c, 0), 0)
        If c < .CountOfLines Then n = n + 1
End With
End Sub

Open in new window

mhopkins9901Author Commented:
Thanks for the help irudyk,
That got me looking in the right direction for a C# solution.

Here is what I came up with for the C# people out there that might need it.
using Microsoft.Vbe.Interop;
List<string> proceduresList = new List<string>();
vbext_ProcKind procType;
foreach (VBComponent vbc in Globals.ThisWorkbook.VBProject.VBComponents)
   CodeModule codeModule = vbc.CodeModule;
   int currentLine = codeModule.CountOfDeclarationLines + 1;
   while (currentLine < codeModule.CountOfLines)
      string procName = codeModule.get_ProcOfLine(currentLine, out procType);
      currentLine += codeModule.get_ProcCountLines(procName, procType);

Open in new window


Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now