通过 VSTO 获取工作簿的所有 VBA 宏
Get all the VBA macros of a workbook by VSTO
在 VBA 中,我们可以使用 Wb.VBProject.VBComponents 来获取工作簿 Wb
.
的所有 VBA 宏
有谁知道 VSTO 是否提供任何对象或函数来访问它?
编辑 1: 根据 Jim 的回答,我得到以下错误:
您需要在项目中添加对 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;
}
}
在 VBA 中,我们可以使用 Wb.VBProject.VBComponents 来获取工作簿 Wb
.
有谁知道 VSTO 是否提供任何对象或函数来访问它?
编辑 1: 根据 Jim 的回答,我得到以下错误:
您需要在项目中添加对 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;
}
}