0% found this document useful (0 votes)
41 views

Using Macros in Csharp

This document contains code to analyze the macros in an Excel workbook. It opens the workbook, loops through each VB component, and extracts information about each macro including the procedure name, lines of code, start line, signature, and any comments above the signature. The extracted information could be used to summarize and document the macros in the workbook.

Uploaded by

zakir_vjti20
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Using Macros in Csharp

This document contains code to analyze the macros in an Excel workbook. It opens the workbook, loops through each VB component, and extracts information about each macro including the procedure name, lines of code, start line, signature, and any comments above the signature. The extracted information could be used to summarize and document the macros in the workbook.

Uploaded by

zakir_vjti20
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System; using Excel = Microsoft.Office.Interop.Excel; using VBA = Microsoft.Vbe.Interop; namespace ClearLines.MacroForensics.

Reader { public class OpenWorkbook { public void Open(string fileName) { 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, Ty pe.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(proce dureName, procedureType); int procedureStartLine = componentCode.get_ProcStartLine(pr ocedureName, procedureType); int codeStartLine = componentCode.get_ProcBodyLine(procedur eName, procedureType); string comments = "[No comments]"; if (codeStartLine != procedureStartLine) { comments = componentCode.get_Lines(line, codeStartLine procedureStartLine); } int signatureLines = 1; while (componentCode.get_Lines(codeStartLine, signatureLine s).EndsWith("_")) { signatureLines++; } string signature = componentCode.get_Lines(codeStartLine, s ignatureLines); signature = signature.Replace("\n", string.Empty); signature = signature.Replace("\r", string.Empty); signature = signature.Replace("_", string.Empty);

line += procedureLines - 1; } line++; } } } excel.Quit(); } } }

You might also like