Exemplo n.º 1
0
        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 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);
                }
            }
        }
Exemplo n.º 3
0
 public int get_ProcStartLine(string ProcName, Microsoft.Vbe.Interop.vbext_ProcKind ProcKind)
 {
     return(_codeModule.get_ProcStartLine(ProcName, ProcKind));
 }
Exemplo n.º 4
0
 public string get_ProcOfLine(int Line, out Microsoft.Vbe.Interop.vbext_ProcKind ProcKind)
 {
     return(_codeModule.get_ProcOfLine(Line, out ProcKind));
 }