Пример #1
0
        private void RegisterComponentsEventSink(VBComponents components, string projectId)
        {
            if (_componentsEventsSinks.ContainsKey(projectId))
            {
                // already registered - this is caused by the initial load+rename of a project in the VBE
                _logger.Debug("Components sink already registered.");
                return;
            }

            var connectionPointContainer = (IConnectionPointContainer)components;
            var interfaceId = typeof(_dispVBComponentsEvents).GUID;

            IConnectionPoint connectionPoint;

            connectionPointContainer.FindConnectionPoint(ref interfaceId, out connectionPoint);

            var componentsSink = new VBComponentsEventsSink();

            componentsSink.ComponentActivated += sink_ComponentActivated;
            componentsSink.ComponentAdded     += sink_ComponentAdded;
            componentsSink.ComponentReloaded  += sink_ComponentReloaded;
            componentsSink.ComponentRemoved   += sink_ComponentRemoved;
            componentsSink.ComponentRenamed   += sink_ComponentRenamed;
            componentsSink.ComponentSelected  += sink_ComponentSelected;
            _componentsEventsSinks.Add(projectId, componentsSink);

            int cookie;

            connectionPoint.Advise(componentsSink, out cookie);

            _componentsEventsConnectionPoints.Add(projectId, Tuple.Create(connectionPoint, cookie));
            _logger.Debug("Components sink registered and advising.");
        }
Пример #2
0
        /// <summary>
        /// Find component by name
        /// </summary>
        /// <param name="components"></param>
        /// <param name="name">component`s name</param>
        /// <returns>null if component not found, otherwise VBComponent</returns>
        public static VBComponent Find(this VBComponents components, string name)
        {
            foreach (VBComponent component in components)
            {
                if (component.Name == name)
                {
                    return(component);
                }
            }

            return(null);
        }
Пример #3
0
 /// <summary>
 /// Safely removes the specified VbComponent from the collection.
 /// </summary>
 /// <remarks>
 /// UserForms, Class modules, and Standard modules are completely removed from the project.
 /// Since Document type components can't be removed through the VBE, all code in its CodeModule are deleted instead.
 /// </remarks>
 public static void RemoveSafely(this VBComponents components, VBComponent component)
 {
     switch (component.Type)
     {
         case vbext_ComponentType.vbext_ct_ClassModule:
         case vbext_ComponentType.vbext_ct_StdModule:
         case vbext_ComponentType.vbext_ct_MSForm:
             components.Remove(component);
             break;
         case vbext_ComponentType.vbext_ct_ActiveXDesigner:
         case vbext_ComponentType.vbext_ct_Document:
             component.CodeModule.Clear();
             break;
         default:
             break;
     }
 }
Пример #4
0
        public static void ImportSourceFile(this VBComponents components, string filePath)
        {
            var ext  = Path.GetExtension(filePath);
            var name = Path.GetFileNameWithoutExtension(filePath);

            if (!File.Exists(filePath))
            {
                return;
            }

            var codeString = File.ReadAllText(filePath);
            var codeLines  = codeString.Split(new [] { Environment.NewLine }, StringSplitOptions.None);

            if (ext == VBComponentExtensions.DocClassExtension)
            {
                var component = components.Item(name);
                component.CodeModule.Clear();
                component.CodeModule.AddFromString(codeString);
            }
            else if (ext == VBComponentExtensions.FormExtension)
            {
                VBComponent component;
                try
                {
                    component = components.Item(name);
                }
                catch (IndexOutOfRangeException)
                {
                    component = components.Add(vbext_ComponentType.vbext_ct_MSForm);
                    component.Properties.Item("Caption").Value = name;
                    component.Name = name;
                }

                var nonAttributeLines     = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
                var attributeLines        = codeLines.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();
                var declarationsStartLine = nonAttributeLines + attributeLines + 1;
                var correctCodeString     = string.Join(Environment.NewLine, codeLines.Skip(declarationsStartLine - 1).ToArray());

                component.CodeModule.Clear();
                component.CodeModule.AddFromString(correctCodeString);
            }
            else if (ext != VBComponentExtensions.FormBinaryExtension)
            {
                components.Import(filePath);
            }
        }
Пример #5
0
        public static void ImportSourceFile(this VBComponents components, string filePath)
        {
            var ext  = Path.GetExtension(filePath);
            var name = Path.GetFileNameWithoutExtension(filePath);

            if (!File.Exists(filePath))
            {
                return;
            }

            var codeString = File.ReadAllText(filePath);
            var codeLines  = codeString.Split(new [] { Environment.NewLine }, StringSplitOptions.None);

            if (ext == VBComponentExtensions.DocClassExtension)
            {
                var component = components.Item(name);
                if (component != null)
                {
                    component.CodeModule.Clear();
                    component.CodeModule.AddFromString(codeString);
                }
            }
            else if (ext != VBComponentExtensions.FormBinaryExtension)
            {
                components.Import(filePath);
            }

            if (ext == VBComponentExtensions.FormExtension)
            {
                var component = components.Item(name);
                // note: vbeCode contains an extraneous line here:
                //var vbeCode = component.CodeModule.Lines().Split(new []{Environment.NewLine}, StringSplitOptions.None);

                var nonAttributeLines     = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
                var attributeLines        = codeLines.Skip(nonAttributeLines).TakeWhile(line => line.StartsWith("Attribute")).Count();
                var declarationsStartLine = nonAttributeLines + attributeLines + 1;
                var correctCodeString     = string.Join(Environment.NewLine, codeLines.Skip(declarationsStartLine - 1).ToArray());

                component.CodeModule.Clear();
                component.CodeModule.AddFromString(correctCodeString);
            }
        }
Пример #6
0
        public static void ImportSourceFile(this VBComponents components, string filePath)
        {
            var ext      = Path.GetExtension(filePath);
            var fileName = Path.GetFileNameWithoutExtension(filePath);

            if (ext == VBComponentExtensions.DocClassExtension)
            {
                var component = components.Item(fileName);
                if (component != null)
                {
                    component.CodeModule.Clear();

                    var text = File.ReadAllText(filePath);
                    component.CodeModule.AddFromString(text);
                }
            }
            else if (ext != VBComponentExtensions.FormBinaryExtension)
            {
                components.Import(filePath);
            }
        }
Пример #7
0
        public void Extract(string fileName, string exportPath)
        {
            int excelProcessID = 0;

            try
            {
                excel          = new Microsoft.Office.Interop.Excel.Application();
                excelProcessID = Process.GetProcessesByName("Excel").OrderByDescending(p => p.Id).Select(p => p.Id)
                                 .ToArray()[0];
                workbooks = excel.Workbooks;
                workbook  = workbooks.Open(fileName, false, true, Type.Missing, Type.Missing, Type.Missing, true,
                                           Type.Missing, Type.Missing, false, false, Type.Missing, false, true, Type.Missing);

                project = workbook.VBProject;
                string       projectName   = project.Name;
                var          procedureType = Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc;
                VBComponents components    = project.VBComponents;
                foreach (VBComponent component in components)
                {
                    VBComponent vbComponent = (VBComponent)component;
                    Properties  props       = vbComponent.Properties;

                    vbComponent.Export($@"C:\Users\mvaysman\Source\Repos\vbaExtensions\{vbComponent.Name}.TXT");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to export VBA Modules");
                throw;
            }
            finally
            {
                excel.Quit();
                Process.GetProcessById(excelProcessID).Kill();
            }
        }
Пример #8
0
        /// <summary>
        /// Select components by type
        /// </summary>
        /// <param name="components"></param>
        /// <param name="type">type to select</param>
        /// <returns></returns>
        public static IEnumerable <VBComponent> Select(this VBComponents components, vbext_ComponentType type)
        {
            IEnumerable <VBComponent> list = components.Cast <VBComponent>();

            return(list.Where(c => c.Type == type));
        }
Пример #9
0
 public IReadOnlyList <string> ComponentNames()
 {
     return(VBComponents.Select(component => component.Name).ToArray());
 }