2016-09-01 86 views

回答

0

您需要参考您的项目添加到Microsoft.Vbe.Interop。您可以使用

Imports Microsoft.Vbe.Interop 

。 。

Dim components As VBComponents = Application.ActiveWorkbook.VBProject.VBComponents 

Dim components As VBComponents = Globals.ThisAddIn.Application.ActiveWorkbook.VBProject.VBComponents 

然后

For Each comp As VBComponent In components 

Next 

编辑:

我在C#创建以下方法来获取宏的列表,并将它们添加到List<string>

private static void GetMacros() 
{ 
    int startLine = 0; 
    vbext_ProcKind ProcKind; 
    List<string> macros = new List<string>(); 

    VBComponents components = Globals.ThisAddIn.Application.ActiveWorkbook.VBProject.VBComponents; 
    try 
    { 
     foreach (VBComponent comp in components) 
     { 
      startLine = comp.CodeModule.CountOfDeclarationLines + 1; 
      while (startLine <= comp.CodeModule.CountOfLines) 
      { 
       string macroName = comp.CodeModule.get_ProcOfLine(startLine, out ProcKind); 
       macros.Add(macroName); 
       startLine += comp.CodeModule.get_ProcCountLines(macroName, ProcKind); 
      } 
     } 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 
+0

感谢您的回答。但是'Dim components As VBComponents = Application.ActiveWorkbook.VBProject.VBComponents'是VBA语句,对吗?请参阅OP中的更新...如何添加对组件的引用Microsoft.Vbe.Interop ...? – SoftTimur

+0

看起来你尝试在加载项“启动”事件中执行此操作。此时“ActiveWorkbook”不可用。我实际上构建了一个加载项,并通过使用按钮单击事件对其进行了测试。在工作簿打开后,您可能会想要这样做。也许在工作簿[启动](https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.startup.aspx)事件中。 –

+1

您应该可以添加项目的参考。 –

相关问题