获取带有描述的Excelmacros名称

我发现使用自动化从Excel获取macros名称的许多方法( 示例 )。 他们大多是相同的,都不能1)检索macros描述(每个录制的macros可以使用Excel UI的描述装饰)或2)过滤掉普通的VBAfunction(有Excellogging的macros,有funcs /macros,你是写自己)。 看起来好像Excel在源代码中保留描述作为评论,但也在一些私人的地方。 如果删除代码注释,说明对Excel仍然可见。

我需要达到至less2),如果可能的话1)也是如此。 我将不胜感激C#或VBA解决scheme,但真的什么都可以做。

如果您在Excel中录制一个macros,添加另一个“手写”到一个模块,然后导出模块到一个文件,你会发现录制的macros有额外的属性,这是从手工input。

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 

你可以使用代码来导出模块,所以你可以parsing导出的文件寻找这些属性。

这是使用VBA访问/操作VBE内容的好资源: http : //www.cpearson.com/excel/vbe.aspx

我遇到了同样的问题,并通过网站的帮助来解决它,你可以通过csharp读取Excel VBAmacros和函数 。 我做了一些改变,并提出了下面的解决scheme。 我收到一个列表,其中包含可用的macros和他们第一次创build时的描述。 我使用正则expression式来parsing描述。 可能是一些更好的解决scheme,但至less它适用于我的目的。

  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; }