public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_WorkingWithProjects();

            Project project = new Project(dataDir + "VbaProject1.mpp");

            VbaProject             vbaProject = project.VbaProject;
            VbaReferenceCollection references = vbaProject.References;

            Console.WriteLine("Reference count ", references.Count);

            VbaReference reference = vbaProject.References.ToList()[0];

            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);

            reference = vbaProject.References.ToList()[1];
            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);

            reference = vbaProject.References.ToList()[2];
            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);
        }
示例#2
0
        public static void CreateVbaProject(string dataDir)
        {
            //ExStart:CreateVbaProject
            Document doc = new Document();

            // Create a new VBA project.
            VbaProject project = new VbaProject();

            project.Name   = "AsposeProject";
            doc.VbaProject = project;

            // Create a new module and specify a macro source code.
            VbaModule module = new VbaModule();

            module.Name       = "AsposeModule";
            module.Type       = VbaModuleType.ProceduralModule;
            module.SourceCode = "New source code";

            // Add module to the VBA project.
            doc.VbaProject.Modules.Add(module);

            doc.Save(dataDir + "VbaProject_out.docm");
            //ExEnd:CreateVbaProject
            Console.WriteLine("\nDocument saved successfully.\nFile saved at " + dataDir);
        }
示例#3
0
        public void CreateNewVbaProject()
        {
            //ExStart
            //ExFor:VbaProject.#ctor
            //ExFor:VbaProject.Name
            //ExFor:VbaModule.#ctor
            //ExFor:VbaModule.Name
            //ExFor:VbaModule.Type
            //ExFor:VbaModule.SourceCode
            //ExFor:VbaModuleCollection.Add(VbaModule)
            //ExFor:VbaModuleType
            //ExSummary:Shows how to create a VbaProject from a scratch for using macros.
            Document doc = new Document();

            // Create a new VBA project
            VbaProject project = new VbaProject();

            project.Name   = "Aspose.Project";
            doc.VbaProject = project;

            // Create a new module and specify a macro source code
            VbaModule module = new VbaModule();

            module.Name = "Aspose.Module";
            // VbaModuleType values:
            // procedural module - A collection of subroutines and functions
            // ------
            // document module - A type of VBA project item that specifies a module for embedded macros and programmatic access
            // operations that are associated with a document
            // ------
            // class module - A module that contains the definition for a new object. Each instance of a class creates
            // a new object, and procedures that are defined in the module become properties and methods of the object
            // ------
            // designer module - A VBA module that extends the methods and properties of an ActiveX control that has been
            // registered with the project
            module.Type       = VbaModuleType.ProceduralModule;
            module.SourceCode = "New source code";

            // Add module to the VBA project
            doc.VbaProject.Modules.Add(module);

            doc.Save(ArtifactsDir + "Document.CreateVBAMacros.docm");
            //ExEnd

            project = new Document(ArtifactsDir + "Document.CreateVBAMacros.docm").VbaProject;

            Assert.AreEqual("Aspose.Project", project.Name);

            VbaModuleCollection modules = doc.VbaProject.Modules;

            Assert.AreEqual(2, modules.Count);

            Assert.AreEqual("ThisDocument", modules[0].Name);
            Assert.AreEqual(VbaModuleType.DocumentModule, modules[0].Type);
            Assert.Null(modules[0].SourceCode);

            Assert.AreEqual("Aspose.Module", modules[1].Name);
            Assert.AreEqual(VbaModuleType.ProceduralModule, modules[1].Type);
            Assert.AreEqual("New source code", modules[1].SourceCode);
        }
示例#4
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);

            //ExStart:ReadReferencesInformation
            Project project = new Project(dataDir + "VbaProject1.mpp");

            VbaProject             vbaProject = project.VbaProject;
            VbaReferenceCollection references = vbaProject.References;

            Console.WriteLine("Reference count " + references.Count);

            VbaReference reference = vbaProject.References.ToList()[0];

            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);

            reference = vbaProject.References.ToList()[1];
            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);

            reference = vbaProject.References.ToList()[2];
            Console.WriteLine("Identifier: " + reference.LibIdentifier);
            Console.WriteLine("Name: " + reference.Name);
            //ExEnd:ReadReferencesInformation
        }
示例#5
0
        public static void Run()
        {
            // The path to the documents directory.
            string     dataDir    = RunExamples.GetDataDir_WorkingWithProjects();
            Project    project    = new Project(dataDir + "VbaProject1.mpp");
            VbaProject vbaProject = project.VbaProject;
            IVbaModule vbaModule  = vbaProject.Modules.ToList()[0];

            Console.WriteLine("Attributes Count: " + vbaModule.Attributes.Count);
            Console.WriteLine("VB_Name: " + vbaModule.Attributes.ToList()[0].Key);
            Console.WriteLine("Module1: " + vbaModule.Attributes.ToList()[0].Value);
        }
        public static void ModifyVbaMacros(string dataDir)
        {
            //ExStart:ModifyVbaMacros
            Document   doc     = new Document(dataDir + "test.docm");
            VbaProject project = doc.VbaProject;

            const string newSourceCode = "Test change source code";

            // Choose a module, and set a new source code.
            project.Modules[0].SourceCode = newSourceCode;
            //ExEnd:ModifyVbaMacros
        }
示例#7
0
        public void CreateNewVbaProject()
        {
            //ExStart
            //ExFor:VbaProject.#ctor
            //ExFor:VbaProject.Name
            //ExFor:VbaModule.#ctor
            //ExFor:VbaModule.Name
            //ExFor:VbaModule.Type
            //ExFor:VbaModule.SourceCode
            //ExFor:VbaModuleCollection.Add(VbaModule)
            //ExFor:VbaModuleType
            //ExSummary:Shows how to create a VbaProject from a scratch for using macros.
            Document doc = new Document();

            // Create a new VBA project
            VbaProject project = new VbaProject();

            project.Name   = "Aspose.Project";
            doc.VbaProject = project;

            // Create a new module and specify a macro source code
            VbaModule module = new VbaModule();

            module.Name       = "Aspose.Module";
            module.Type       = VbaModuleType.ProceduralModule;
            module.SourceCode = "New source code";

            // Add module to the VBA project
            doc.VbaProject.Modules.Add(module);

            doc.Save(ArtifactsDir + "VbaProject.CreateVBAMacros.docm");
            //ExEnd

            project = new Document(ArtifactsDir + "VbaProject.CreateVBAMacros.docm").VbaProject;

            Assert.AreEqual("Aspose.Project", project.Name);

            VbaModuleCollection modules = doc.VbaProject.Modules;

            Assert.AreEqual(2, modules.Count);

            Assert.AreEqual("ThisDocument", modules[0].Name);
            Assert.AreEqual(VbaModuleType.DocumentModule, modules[0].Type);
            Assert.Null(modules[0].SourceCode);

            Assert.AreEqual("Aspose.Module", modules[1].Name);
            Assert.AreEqual(VbaModuleType.ProceduralModule, modules[1].Type);
            Assert.AreEqual("New source code", modules[1].SourceCode);
        }
        public void ModifyVbaMacros()
        {
            //ExStart:ModifyVbaMacros
            Document doc = new Document(MyDir + "VBA project.docm");

            VbaProject project = doc.VbaProject;

            const string newSourceCode = "Test change source code";

            project.Modules[0].SourceCode = newSourceCode;
            //ExEnd:ModifyVbaMacros

            doc.Save(ArtifactsDir + "WorkingWithVba.ModifyVbaMacros.docm");
            //ExEnd:ModifyVbaMacros
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);

            // ExStart:ReadModuleAttributesInformation
            Project    project    = new Project(dataDir + "VbaProject1.mpp");
            VbaProject vbaProject = project.VbaProject;
            IVbaModule vbaModule  = vbaProject.Modules.ToList()[0];

            Console.WriteLine("Attributes Count: " + vbaModule.Attributes.Count);
            Console.WriteLine("VB_Name: " + vbaModule.Attributes.ToList()[0].Key);
            Console.WriteLine("Module1: " + vbaModule.Attributes.ToList()[0].Value);
            // ExEnd:ReadModuleAttributesInformation
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Projects();

            // loading project file
            Project project = new Project(dataDir + "VbaProjectask1.mpp");

            VbaProject vbaProject = project.VbaProject;

            Console.WriteLine("VbaProject.Name " + vbaProject.Name);
            Console.WriteLine("VbaProject.Description " + vbaProject.Description);
            Console.WriteLine("VbaProject.CompilationArguments" + vbaProject.CompilationArguments);
            Console.WriteLine("VbaProject.HelpContextId" + vbaProject.HelpContextId);
        }
示例#11
0
        public static void CloneVbaProject(string dataDir)
        {
            //ExStart:CloneVbaProject
            Document   doc     = new Document(dataDir + "VbaProject_source.docm");
            VbaProject project = doc.VbaProject;

            Document destDoc = new Document();

            // Clone the whole project.
            destDoc.VbaProject = doc.VbaProject.Clone();

            destDoc.Save(dataDir + "output.docm");
            //ExEnd:CloneVbaProject
            Console.WriteLine("\nDocument saved successfully.\nFile saved at " + dataDir);
        }
示例#12
0
        public static void ModifyVbaMacros(string dataDir)
        {
            //ExStart:ModifyVbaMacros
            Document   doc     = new Document(dataDir + "test.docm");
            VbaProject project = doc.VbaProject;

            const string newSourceCode = "Test change source code";

            // Choose a module, and set a new source code.
            project.Modules[0].SourceCode = newSourceCode;
            //ExEnd:ModifyVbaMacros

            doc.Save(dataDir + "VbaProject_out.docm");
            Console.WriteLine("\nDocument saved successfully.\nFile saved at " + dataDir);
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_WorkingWithProjects();

            Project project = new Project(dataDir + "VbaProject1.mpp");

            VbaProject vbaProject = project.VbaProject;

            Console.WriteLine("Total Modules Count: " + vbaProject.Modules.Count);

            IVbaModule vbaModule = vbaProject.Modules.ToList()[0];

            Console.WriteLine("Module Name: " + vbaModule.Name);
            Console.WriteLine("Source Code: " + vbaModule.SourceCode);
        }
示例#14
0
        static void Main()
        {
            //ExStart:1
            string dataDir    = Aspose.Cells.Examples.Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            string outputPath = dataDir + "Output.out.xlsm";

            Workbook workbook = new Workbook();

            VbaProject vbaProj = workbook.VbaProject;

            vbaProj.References.AddRegisteredReference("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");
            vbaProj.References.AddRegisteredReference("Office", "*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library");

            workbook.Save(outputPath);
            //ExEnd:1
        }
示例#15
0
        public static void Run()
        {
            // ExStart:1
            string dataDir    = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            string outputPath = dataDir + "Output_out_.xlsm";

            Workbook workbook = new Workbook();

            VbaProject vbaProj = workbook.VbaProject;

            vbaProj.References.AddRegisteredReference("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");
            vbaProj.References.AddRegisteredReference("Office", "*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library");

            workbook.Save(outputPath);
            // ExEnd:1
            Console.WriteLine("\nProcess completed successfully.\nFile saved at " + outputPath);
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);

            // ExStart:ReadVBAProjectInformation
            // Loading project file
            Project project = new Project(dataDir + "VbaProject1.mpp");

            VbaProject vbaProject = project.VbaProject;

            Console.WriteLine("VbaProject.Name " + vbaProject.Name);
            Console.WriteLine("VbaProject.Description " + vbaProject.Description);
            Console.WriteLine("VbaProject.CompilationArguments" + vbaProject.CompilationArguments);
            Console.WriteLine("VbaProject.HelpContextId" + vbaProject.HelpContextId);
            // ExEnd:ReadVBAProjectInformation
        }
示例#17
0
        internal static IList <CodeAnalyzerResult> AnalyzeProject(VbaProject model)
        {
            MyLogger.Debug($"Analyzing {model.FileName} with {model.VbaModules.Count} VBA modules.");
            Stopwatch sw = Stopwatch.StartNew();
            IList <CodeAnalyzerResult> results;

            try
            {
                results = new CodeAnalyzer(model.FileName).Run(model.VbaModules.ToDictionary(x => x.Name, x => x.Code));
            }
            catch (Exception exception)
            {
                MyLogger.Error(exception);
                throw;
            }
            MyLogger.Debug($"Analysis took {sw.ElapsedMilliseconds}ms.");
            return(results);
        }
示例#18
0
        public static void CloneVbaModule(string dataDir)
        {
            //ExStart:CloneVbaModule
            Document   doc     = new Document(dataDir + "VbaProject_source.docm");
            VbaProject project = doc.VbaProject;

            Document destDoc = new Document();

            destDoc.VbaProject = new VbaProject();

            // Clone a single module.
            VbaModule copyModule = doc.VbaProject.Modules["Module1"].Clone();

            destDoc.VbaProject.Modules.Add(copyModule);

            destDoc.Save(dataDir + "output.docm");
            //ExEnd:CloneVbaModule
            Console.WriteLine("\nDocument saved successfully.\nFile saved at " + dataDir);
        }
示例#19
0
        public void CloneVbaProject()
        {
            //ExStart
            //ExFor:VbaProject.Clone
            //ExFor:VbaModule.Clone
            //ExSummary:Shows how to deep clone VbaProject and VbaModule.
            Document doc     = new Document(MyDir + "VBA project.docm");
            Document destDoc = new Document();

            // Clone VbaProject to the document
            VbaProject copyVbaProject = doc.VbaProject.Clone();

            destDoc.VbaProject = copyVbaProject;

            // In destination document we already have "Module1", because it was cloned with VbaProject
            // We will need to remove it before cloning
            VbaModule oldVbaModule  = destDoc.VbaProject.Modules["Module1"];
            VbaModule copyVbaModule = doc.VbaProject.Modules["Module1"].Clone();

            destDoc.VbaProject.Modules.Remove(oldVbaModule);
            destDoc.VbaProject.Modules.Add(copyVbaModule);

            destDoc.Save(ArtifactsDir + "VbaProject.CloneVbaProject.docm");
            //ExEnd

            VbaProject originalVbaProject = new Document(ArtifactsDir + "VbaProject.CloneVbaProject.docm").VbaProject;

            Assert.AreEqual(copyVbaProject.Name, originalVbaProject.Name);
            Assert.AreEqual(copyVbaProject.CodePage, originalVbaProject.CodePage);
            Assert.AreEqual(copyVbaProject.IsSigned, originalVbaProject.IsSigned);
            Assert.AreEqual(copyVbaProject.Modules.Count, originalVbaProject.Modules.Count);

            for (int i = 0; i < originalVbaProject.Modules.Count; i++)
            {
                Assert.AreEqual(copyVbaProject.Modules[i].Name, originalVbaProject.Modules[i].Name);
                Assert.AreEqual(copyVbaProject.Modules[i].Type, originalVbaProject.Modules[i].Type);
                Assert.AreEqual(copyVbaProject.Modules[i].SourceCode, originalVbaProject.Modules[i].SourceCode);
            }
        }
        public void CreateVbaProject()
        {
            //ExStart:CreateVbaProject
            Document doc = new Document();

            VbaProject project = new VbaProject();

            project.Name   = "AsposeProject";
            doc.VbaProject = project;

            // Create a new module and specify a macro source code.
            VbaModule module = new VbaModule();

            module.Name       = "AsposeModule";
            module.Type       = VbaModuleType.ProceduralModule;
            module.SourceCode = "New source code";

            // Add module to the VBA project.
            doc.VbaProject.Modules.Add(module);

            doc.Save(ArtifactsDir + "WorkingWithVba.CreateVbaProject.docm");
            //ExEnd:CreateVbaProject
        }
示例#21
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (_vba == null && !_package.Package.PartExists(new Uri(ExcelVbaProject.PartUri, UriKind.Relative)))
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookDefault;
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookMacroEnabled;
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
                worksheet.Part.SaveHandler = worksheet.SaveHandler;
            }

            // Issue 15252: save SharedStrings only once
            Packaging.ZipPackagePart part;
            if (_package.Package.PartExists(SharedStringsUri))
            {
                part = _package.Package.GetPart(SharedStringsUri);
            }
            else
            {
                part = _package.Package.CreatePart(SharedStringsUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", _package.Compression);
                Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings");
            }

            part.SaveHandler = SaveSharedStringHandler;
            //UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (_vba != null)
            {
                VbaProject.Save();
            }
        }
示例#22
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (VbaProject == null)
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    ChangeContentTypeWorkbook(ExcelPackage.contentTypeWorkbookDefault);
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    ChangeContentTypeWorkbook(ExcelPackage.contentTypeWorkbookMacroEnabled);
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
            }

            UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (VbaProject != null)
            {
                VbaProject.Save();
            }
        }
示例#23
0
        /// <summary>
        /// Saves the workbook and all its components to the package.
        /// For internal use only!
        /// </summary>
        internal void Save()          // Workbook Save
        {
            if (Worksheets.Count == 0)
            {
                throw new InvalidOperationException("The workbook must contain at least one worksheet");
            }

            DeleteCalcChain();

            if (VbaProject == null)
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookDefault)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookDefault;
                }
            }
            else
            {
                if (Part.ContentType != ExcelPackage.contentTypeWorkbookMacroEnabled)
                {
                    Part.ContentType = ExcelPackage.contentTypeWorkbookMacroEnabled;
                }
            }

            UpdateDefinedNamesXml();

            // save the workbook
            if (_workbookXml != null)
            {
                _package.SavePart(WorkbookUri, _workbookXml);
            }

            // save the properties of the workbook
            if (_properties != null)
            {
                _properties.Save();
            }

            // save the style sheet
            Styles.UpdateXml();
            _package.SavePart(StylesUri, _stylesXml);

            // save all the open worksheets
            var isProtected = Protection.LockWindows || Protection.LockStructure;

            foreach (ExcelWorksheet worksheet in Worksheets)
            {
                if (isProtected && Protection.LockWindows)
                {
                    worksheet.View.WindowProtection = true;
                }
                worksheet.Save();
                worksheet.Part.SaveHandler = worksheet.SaveHandler;
            }

            var part = _package.Package.CreatePart(SharedStringsUri, ExcelPackage.contentTypeSharedString, _package.Compression);

            part.SaveHandler = SaveSharedStringHandler;
            Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings");
            //UpdateSharedStringsXml();

            // Data validation
            ValidateDataValidations();

            //VBA
            if (_vba != null)
            {
                VbaProject.Save();
            }
        }