通过EPPlus获取工作簿的所有VBA宏



在从EPPlus下载的示例Sample15.cs中,我看到EPPlus可以编写/注入VBA宏。但是,我不知道如何从.xlsm工作簿中读取所有的VBA宏。

EPPlus能解释VbaProject.bin并支持此功能吗?

PS:似乎Open XML SDK也不能这样做(请纠正我,如果我错了),这就是为什么我正在考虑EPPlus…

如果你只是想在每个模块中阅读代码,你只需要通过'Workbook.VbaProject。模块的集合如下:

var fi = new FileInfo(@"C:tempBook1.xlsm");
using (var pck = new ExcelPackage(fi))
{
    var modules = pck.Workbook.VbaProject.Modules;
    foreach (var module in modules)
    {
        Console.WriteLine($"Module Name: {module.Name}{Environment.NewLine}Code:");
        Console.Write(module.Code);
        Console.WriteLine("---------");
    }

这将在输出中给你这个(我刚刚创建了一个带有两个模块的excel xlsm):

Module Name: ThisWorkbook
Code:
---------
Module Name: Sheet1
Code:
---------
Module Name: Module1
Code:
Public Sub proc1()
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Dim ws As Worksheet
    Set ws = wb.ActiveSheet
    ws.Cells(1, 1).Value = "proc1"
End Sub
---------
Module Name: Module2
Code:
Public Sub proc2()
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Dim ws As Worksheet
    Set ws = wb.ActiveSheet
    ws.Cells(2, 1).Value = "proc2"
End Sub
---------

相关内容

  • 没有找到相关文章

最新更新