Esempio n. 1
0
 private static void OnProjectActivated(VB.VBProject vbProject)
 {
     if (IsInDesignMode() && vbProject.Protection == VB.vbext_ProjectProtection.vbext_pp_none)
     {
         OnDispatch(ProjectActivated, vbProject);
     }
 }
        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;
                    }
                }
            }
        }
Esempio n. 3
0
        private static void AddModule(Excel.Workbook xlWbk, string source)
        {
            xlWbk.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityLow;
            Excel.Application       xlApp           = xlWbk.Application;
            VBE.VBProject           vbProject       = xlWbk.VBProject;
            VBE.vbext_ComponentType vbComponentType = VBE.vbext_ComponentType.vbext_ct_StdModule;
            VBE.VBComponent         vbModule        = vbProject.VBComponents.Add(vbComponentType);
            VBE.CodeModule          vbCode          = vbModule.CodeModule;

            // Adds code to the module
            vbCode.Name = Path.GetFileName(source).Split(".")[0];
            vbCode.InsertLines(1, File.ReadAllText(source));
            xlApp.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityByUI;

            // Clean up
            while (Marshal.ReleaseComObject(vbProject) != 0)
            {
            }
            while (Marshal.ReleaseComObject(vbModule) != 0)
            {
            }
            while (Marshal.ReleaseComObject(vbCode) != 0)
            {
            }
        }
        public VBProjectConnector(VBA.VBProject vbproj)
        {
            VBProj  = vbproj;
            VBComps = vbproj.VBComponents;
            //VBA.VBComponent newStandardModule = VBProj.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule);

            //newStandardModule.CodeModule.Name = "ABC";
            Update();
            return;
        }
        private void ParseVBMacro(string filePath)
        {
            Excel.Workbook workBook = null;

            try
            {
                if (this.excelApp != null)
                {
                    workBook = this.excelApp.Workbooks.Open(filePath);

                    // Check if we have VB macros or not.
                    if (workBook.HasVBProject)
                    {
                        // Get the project.
                        VBA.VBProject project = workBook.VBProject;

                        // Process each component in project.
                        foreach (VBA.VBComponent component in project.VBComponents)
                        {
                            ParseVBComponent(filePath, component);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Dump the message on console.
                Console.WriteLine(e.Message);

                // Write the actual exception message to log file.
                File.AppendAllText(errorFile, e.Message + Environment.NewLine);
            }
            finally
            {
                // Close the workbook without saving anything.
                if (workBook != null)
                {
                    workBook.Close(false);
                }

                if (workBook != null)
                {
                    Marshal.ReleaseComObject(workBook);
                }
            }
        }
Esempio n. 6
0
        private static void OnProjectRenamed(VB.VBProject vbProject, string oldName)
        {
            if (!IsInDesignMode() || vbProject.Protection == VB.vbext_ProjectProtection.vbext_pp_locked)
            {
                return;
            }

            var project   = new VBProject(vbProject);
            var projectId = project.ProjectId;

            var handler = ProjectRenamed;

            if (handler != null && projectId != null)
            {
                handler.Invoke(project, new ProjectRenamedEventArgs(projectId, project, oldName));
            }
        }
Esempio n. 7
0
        private static void OnDispatch(EventHandler <ProjectEventArgs> dispatched, VB.VBProject vbProject, bool assignId = false)
        {
            var handler = dispatched;

            if (handler != null && vbProject.Protection != VB.vbext_ProjectProtection.vbext_pp_locked)
            {
                var project = new VBProject(vbProject);
                if (assignId)
                {
                    project.AssignProjectId();
                }
                var projectId = project.ProjectId;
                if (projectId != null)
                {
                    handler.Invoke(project, new ProjectEventArgs(projectId, project));
                }
            }
        }
Esempio n. 8
0
        private void button13_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application eApp = new Microsoft.Office.Interop.Excel.Application();
            eApp.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook workBook = eApp.Workbooks.Add();
            Microsoft.Vbe.Interop.VBProject         project  = null;

            try
            {
                project = workBook.VBProject;
            }
            catch (Exception ex)
            {
                StringBuilder sbMes = new StringBuilder();
                sbMes.AppendLine("  Please enable \"Trust access to VBA project model\" in Microsoft Excel .");
                sbMes.AppendLine();
                sbMes.AppendLine("      1. File > Option > Trust Center ");
                sbMes.AppendLine("      2. Click to 'Trust Center Setting...' ");
                sbMes.AppendLine("      3. Click to 'Macro Setting' tab");
                sbMes.AppendLine("      4. Check the box 'Trust access to VBA project model' ");
                sbMes.AppendLine("      5. Click 'OK'");

                MessageBox.Show(sbMes.ToString());
            }
            var           projectName   = project.Name;
            var           procedureType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc;
            List <string> lstMacro      = new List <string>();

            foreach (var component in project.VBComponents)
            {
                VBComponent vbComponent = component as VBComponent;
                if (vbComponent != null)
                {
                    string componentName = vbComponent.Name;
                    var    componentCode = vbComponent.CodeModule;
                    bool   saved         = vbComponent.Saved;
                    bool   openDesigner  = vbComponent.HasOpenDesigner;

                    int componentCodeLines = componentCode.CountOfLines;
                }
            }
        }
Esempio n. 9
0
        void VB._dispVBProjectsEvents.ItemRenamed([MarshalAs(UnmanagedType.Interface), In] VB.VBProject VBProject,
                                                  [MarshalAs(UnmanagedType.BStr), In] string OldName)
        {
            var project = new VBProject(VBProject);

            if (!IsInDesignMode() || VBProject.Protection == VB.vbext_ProjectProtection.vbext_pp_locked)
            {
                project.Dispose();
                return;
            }

            var projectId = project.ProjectId;

            var handler = ProjectRenamed;

            if (handler == null || projectId == null)
            {
                project.Dispose();
                return;
            }
            handler.Invoke(project, new ProjectRenamedEventArgs(projectId, project, OldName));
        }
Esempio n. 10
0
        private void OnDispatch(EventHandler <ProjectEventArgs> dispatched, VB.VBProject vbProject, bool assignId = false)
        {
            var project = new VBProject(vbProject);
            var handler = dispatched;

            if (handler == null || !IsInDesignMode() || vbProject.Protection == VB.vbext_ProjectProtection.vbext_pp_locked)
            {
                project.Dispose();
                return;
            }

            if (assignId)
            {
                project.AssignProjectId();
            }
            var projectId = project.ProjectId;

            if (projectId == null)
            {
                project.Dispose();
                return;
            }
            handler.Invoke(project, new ProjectEventArgs(projectId, project));
        }
Esempio n. 11
0
 void VB._dispVBProjectsEvents.ItemRemoved([MarshalAs(UnmanagedType.Interface), In] VB.VBProject VBProject)
 {
     OnDispatch(ProjectRemoved, VBProject);
 }
Esempio n. 12
0
 void VB._dispVBProjectsEvents.ItemAdded([MarshalAs(UnmanagedType.Interface), In] VB.VBProject VBProject)
 {
     OnDispatch(ProjectAdded, VBProject, true);
 }