简体   繁体   English

获取带有描述的 Excel 宏名称

[英]Get Excel macros names with descriptions

I have found many ways for getting macro names from Excel using automation ( example ).我找到了许多使用自动化从 Excel 获取宏名称的方法( 示例)。 They are mostly the same and neither can 1) retrieve macro descriptions (each recorded macro can be decorated with a description using Excel UI) or 2) filter out ordinary VBA functions (there are Excel recorded macros and there are funcs/macros that you an write yourself).它们大多相同,也不能 1)检索宏描述(每个记录的宏可以使用 Excel UI 用描述修饰)或 2)过滤掉普通的 VBA 函数(有 ZC1D81AF5835844B4E9D936910 函数是记录的自己写)。 It seems as if Excel is keeping descriptions as comments in the source code but also in some private place too.似乎 Excel 将描述作为注释保留在源代码中,但也在某些私人地方。 If one deletes code comments the description is still visible to Excel.如果删除代码注释,描述仍然对 Excel 可见。

I need to achieve at least 2) and if possible 1) too.我至少需要达到 2),如果可能的话,也需要达到 1)。 I'd appreciate a C# or VBA solution, but really anything will do.我很欣赏 C# 或 VBA 解决方案,但实际上任何事情都可以。

I encountered the same problem and solved it by help of a website where you could read Excel VBA macros and functions through csharp .我遇到了同样的问题,并通过一个网站解决了这个问题,您可以通过 csharp 阅读 Excel VBA 宏和函数 I made some changes and came up with the solution seen below.我进行了一些更改,并提出了如下所示的解决方案。 I receive a list with the available macros and the description they got when they first where created.我收到一个列表,其中包含可用的宏以及它们在第一次创建时获得的描述。 I use regex to parse the description.我使用正则表达式来解析描述。 Might be some better solution for this, but at least it works for my purpose.可能是一些更好的解决方案,但至少它适用于我的目的。

    public List<Tuple<string,string>> GetAllMacrosInExcelFile(string fileName) {

        List<Tuple<string,string>> listOfMacros = new List<Tuple<string,string>>();

        var excel = new Excel.Application();
        var workbook = excel.Workbooks.Open(fileName, false, true, Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, false, false, Type.Missing, false, true, Type.Missing);

        var project = workbook.VBProject;
        var projectName = project.Name;
        var procedureType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc;

        foreach (var component in project.VBComponents) {
            VBA.VBComponent vbComponent = component as VBA.VBComponent;
            if (vbComponent != null) {
                string componentName = vbComponent.Name;
                var componentCode = vbComponent.CodeModule;
                int componentCodeLines = componentCode.CountOfLines;

                int line = 1;
                while (line < componentCodeLines) {
                    string procedureName = componentCode.get_ProcOfLine(line, out procedureType);
                    if (procedureName != string.Empty) {
                        int procedureLines = componentCode.get_ProcCountLines(procedureName, procedureType);
                        int procedureStartLine = componentCode.get_ProcStartLine(procedureName, procedureType);
                        var allCodeLines = componentCode.get_Lines(procedureStartLine, procedureLines);

                        Regex regex = new Regex("Macro\r\n' (.*?)\r\n'\r\n\r\n'");
                        var v = regex.Match(allCodeLines);
                        string comments = v.Groups[1].ToString();

                        if (comments.IsEmpty()) { comments = "No comment is written for this Macro"; }

                        line += procedureLines - 1;
                        listOfMacros.Add(procedureName.Tuple(comments));
                    } 
                    line++;
                }
            }
        }
        excel.Quit();
        return listOfMacros;
    }

If you record a macro in excel, add another "hand-written" one to the same module, and then export the module to a file, you'll see that the recorded macro has additional Attributes which are missing from the hand-entered one.如果您在 excel 中录制一个宏,在同一个模块中添加另一个“手写”宏,然后将模块导出到文件,您会看到录制的宏具有额外的属性,而这些属性是手动输入的.

Sub RecordedMacro()
Attribute RecordedMacro.VB_Description = "some description here"
Attribute RecordedMacro.VB_ProcData.VB_Invoke_Func = "g\n14"
'
' RecordedMacro Macro
' some description here
'
' Keyboard Shortcut: Ctrl+g
'
    Range("C8").Select
    ActiveCell.FormulaR1C1 = "sfhsf"

End Sub

You can use code to export modules, so you could parse the exported files looking for those attributes.您可以使用代码导出模块,因此您可以解析导出的文件以查找这些属性。

This is a good resource for using VBA to access/manipulate contents of the VBE: http://www.cpearson.com/excel/vbe.aspx这是使用 VBA 访问/操作 VBE 内容的好资源: http://www.cpearson.com/excel/vbe.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM