private void frmMacros_Load(object sender, EventArgs e) { wb = xl.ActiveWorkbook; proj = wb.VBProject; var projName = proj.Name; projType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc; cmbMacrosNames.Items.Clear(); foreach (var item in proj.VBComponents) { vbide.VBComponent vbComponent = item as vbide.VBComponent; if (vbComponent != null) { string componentName = vbComponent.Name; vbide.CodeModule comCode = vbComponent.CodeModule; int comCodeLines = comCode.CountOfLines; int line = 1; while (line <= comCodeLines) { string proceName = comCode.get_ProcOfLine(line, out projType); if (line == comCode.get_ProcStartLine(proceName, projType)) { if (proceName != null) { cmbMacrosNames.Items.Add(proceName); } } line = line + 1; } } } }
private void btnCreateMacro_Click(object sender, EventArgs e) { foreach (vbide.VBComponent item in proj.VBComponents) { vbide.VBComponent Md = item as vbide.VBComponent; vbide.CodeModule MdCode = Md.CodeModule; if (MdCode != null) { if (MdCode.get_ProcOfLine(1, out projType) == txtMacroName.Text) { MdCode.DeleteLines(1, Md.CodeModule.get_ProcCountLines(txtMacroName.Text, projType)); } } } vbide.VBComponent Mdn; Mdn = proj.VBComponents.Add(vbide.vbext_ComponentType.vbext_ct_StdModule); string sCode = ""; sCode = "public Sub " + txtMacroName.Text + "() \n"; int LC = txtVBACode.Lines.Count(); for (int i = 0; i < LC; i++) { sCode += "" + txtVBACode.Lines[i].ToString() + "\n"; } Mdn.CodeModule.AddFromString(sCode); cmbMacrosNames.Items.Clear(); foreach (var item2 in proj.VBComponents) { vbide.VBComponent vbComponent = item2 as vbide.VBComponent; if (vbComponent != null) { string componentName = vbComponent.Name; vbide.CodeModule comCode = vbComponent.CodeModule; int comCodeLines = comCode.CountOfLines; int line = 1; while (line <= comCodeLines) { string proceName = comCode.get_ProcOfLine(line, out projType); if (line == comCode.get_ProcStartLine(proceName, projType)) { if (proceName != null) { cmbMacrosNames.Items.Add(proceName); } } line = line + 1; } } } MessageBox.Show(txtMacroName.Text + " Macro has been saved successfully"); }
private void ParseVBComponent(string filePath, VBA.VBComponent component) { if (component != null) { // Get the file name prefix to remove. string filePrefix = ConfigurationManager.AppSettings["RemoveNamePrefix"]; VBA.vbext_ProcKind procedureType = VBA.vbext_ProcKind.vbext_pk_Proc; VBA.CodeModule componentCode = component.CodeModule; // Clear out the containers. macroOperations.Clear(); macroCellFormula.Clear(); string procedureName = ""; for (int line = 1; line < componentCode.CountOfLines; line++) { // Name of the macro procedure. 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); string procedureBody = componentCode.get_Lines(procedureStartLine, procedureLines); CompilationUnitSyntax root = VisualBasicSyntaxTree.ParseText(procedureBody).GetRoot() as CompilationUnitSyntax; /* TODO: If we wanted to iterate through the statements of the sub block. * // Get the statements within the sub block. * MethodBlockSyntax macro = root.Members[0] as MethodBlockSyntax; * SyntaxList<StatementSyntax> statements = (SyntaxList<StatementSyntax>)macro.Statements; * * int statementCount = 0; * foreach (StatementSyntax statement in statements) * { * Console.WriteLine(statement.Kind().ToString()); * Console.WriteLine(i.ToString() + " : " + statement.ToString()); * statementCount++; * } */ // Do the parsing inside the syntax walker. this.Visit(root); line += procedureLines - 1; } } if (macroCellFormula.Count > 0 || macroOperations.Count > 0) { // Add the file name. macroInfoBuilder.Append(filePath.Replace(filePrefix, "")); macroInfoBuilder.Append("," + procedureName); // Check if we have information for this component and add them. if (macroCellFormula.Count > 0) { macroInfoBuilder.Append("," + EscapeCsvData(string.Join(", ", macroCellFormula))); } else { macroInfoBuilder.Append(","); } if (macroOperations.Count > 0) { macroInfoBuilder.Append("," + EscapeCsvData(string.Join(", ", macroOperations))); } else { macroInfoBuilder.Append(","); } macroInfoBuilder.Append(Environment.NewLine); } } }