Пример #1
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);
            }
        }
Пример #2
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);
            }
        }
Пример #3
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);
            }
        }