com.aspose.words

Class VbaModule

  • java.lang.Object
    • com.aspose.words.VbaModule
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class VbaModule 
    extends java.lang.Object

Provides access to VBA project module.

Example:

Shows how to get access to VBA project information in the document.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project inside the document is defined as a collection of VBA modules
VbaProject vbaProject = doc.getVbaProject();
System.out.println(vbaProject.isSigned()
        ? MessageFormat.format("Project name: {0} signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount())
        : MessageFormat.format("Project name: {0} not signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount()));

VbaModuleCollection vbaModules = doc.getVbaProject().getModules(); 

Assert.assertEquals(vbaModules.getCount(), 3);

for (VbaModule module : vbaModules) {
    System.out.println(MessageFormat.format("Module name: {0};\nModule code:\n{1}\n", module.getName(), module.getSourceCode()));
}

// Set new source code for VBA module
// You can retrieve object by integer or by name
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove one of VbaModule from VbaModuleCollection
vbaModules.remove(vbaModules.get(2));

Constructor Summary
VbaModule()
Creates an empty module.
 
Property Getters/Setters Summary
java.lang.StringgetName()
void
setName(java.lang.Stringvalue)
           Gets or sets VBA project module name.
java.lang.StringgetSourceCode()
void
setSourceCode(java.lang.Stringvalue)
           Gets or sets VBA project module source code.
intgetType()
void
setType(intvalue)
           Specifies whether the module is a procedural module, document module, class module, or designer module. The value of the property is VbaModuleType integer constant.
 
Method Summary
VbaModuledeepClone()
Performs a copy of the VbaModule.
 

    • Constructor Detail

      • VbaModule

        public VbaModule()
        Creates an empty module.

        Example:

        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.setName("Aspose.Project");
        doc.setVbaProject(project);
        
        // Create a new module and specify a macro source code
        VbaModule module = new VbaModule();
        module.setName("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.setType(VbaModuleType.PROCEDURAL_MODULE);
        module.setSourceCode("New source code");
        
        // Add module to the VBA project
        doc.getVbaProject().getModules().add(module);
        
        doc.save(getArtifactsDir() + "Document.CreateVBAMacros.docm");
    • Property Getters/Setters Detail

      • getName/setName

        public java.lang.String getName() / public void setName(java.lang.String value)
        
        Gets or sets VBA project module name.

        Example:

        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.setName("Aspose.Project");
        doc.setVbaProject(project);
        
        // Create a new module and specify a macro source code
        VbaModule module = new VbaModule();
        module.setName("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.setType(VbaModuleType.PROCEDURAL_MODULE);
        module.setSourceCode("New source code");
        
        // Add module to the VBA project
        doc.getVbaProject().getModules().add(module);
        
        doc.save(getArtifactsDir() + "Document.CreateVBAMacros.docm");

        Example:

        Shows how to get access to VBA project information in the document.
        Document doc = new Document(getMyDir() + "VBA project.docm");
        
        // A VBA project inside the document is defined as a collection of VBA modules
        VbaProject vbaProject = doc.getVbaProject();
        System.out.println(vbaProject.isSigned()
                ? MessageFormat.format("Project name: {0} signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount())
                : MessageFormat.format("Project name: {0} not signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount()));
        
        VbaModuleCollection vbaModules = doc.getVbaProject().getModules(); 
        
        Assert.assertEquals(vbaModules.getCount(), 3);
        
        for (VbaModule module : vbaModules) {
            System.out.println(MessageFormat.format("Module name: {0};\nModule code:\n{1}\n", module.getName(), module.getSourceCode()));
        }
        
        // Set new source code for VBA module
        // You can retrieve object by integer or by name
        vbaModules.get(0).setSourceCode("Your VBA code...");
        vbaModules.get("Module1").setSourceCode("Your VBA code...");
        
        // Remove one of VbaModule from VbaModuleCollection
        vbaModules.remove(vbaModules.get(2));
      • getSourceCode/setSourceCode

        public java.lang.String getSourceCode() / public void setSourceCode(java.lang.String value)
        
        Gets or sets VBA project module source code.

        Example:

        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.setName("Aspose.Project");
        doc.setVbaProject(project);
        
        // Create a new module and specify a macro source code
        VbaModule module = new VbaModule();
        module.setName("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.setType(VbaModuleType.PROCEDURAL_MODULE);
        module.setSourceCode("New source code");
        
        // Add module to the VBA project
        doc.getVbaProject().getModules().add(module);
        
        doc.save(getArtifactsDir() + "Document.CreateVBAMacros.docm");

        Example:

        Shows how to get access to VBA project information in the document.
        Document doc = new Document(getMyDir() + "VBA project.docm");
        
        // A VBA project inside the document is defined as a collection of VBA modules
        VbaProject vbaProject = doc.getVbaProject();
        System.out.println(vbaProject.isSigned()
                ? MessageFormat.format("Project name: {0} signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount())
                : MessageFormat.format("Project name: {0} not signed; Project code page: {1}; Modules count: {2}\n", vbaProject.getName(), vbaProject.getCodePage(), vbaProject.getModules().getCount()));
        
        VbaModuleCollection vbaModules = doc.getVbaProject().getModules(); 
        
        Assert.assertEquals(vbaModules.getCount(), 3);
        
        for (VbaModule module : vbaModules) {
            System.out.println(MessageFormat.format("Module name: {0};\nModule code:\n{1}\n", module.getName(), module.getSourceCode()));
        }
        
        // Set new source code for VBA module
        // You can retrieve object by integer or by name
        vbaModules.get(0).setSourceCode("Your VBA code...");
        vbaModules.get("Module1").setSourceCode("Your VBA code...");
        
        // Remove one of VbaModule from VbaModuleCollection
        vbaModules.remove(vbaModules.get(2));
      • getType/setType

        public int getType() / public void setType(int value)
        
        Specifies whether the module is a procedural module, document module, class module, or designer module. The value of the property is VbaModuleType integer constant.

        Example:

        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.setName("Aspose.Project");
        doc.setVbaProject(project);
        
        // Create a new module and specify a macro source code
        VbaModule module = new VbaModule();
        module.setName("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.setType(VbaModuleType.PROCEDURAL_MODULE);
        module.setSourceCode("New source code");
        
        // Add module to the VBA project
        doc.getVbaProject().getModules().add(module);
        
        doc.save(getArtifactsDir() + "Document.CreateVBAMacros.docm");
    • Method Detail

      • deepClone

        public VbaModule deepClone()
        Performs a copy of the VbaModule.
        Returns:
        The cloned VbaModule.

        Example:

        Shows how to deep clone VbaProject and VbaModule.
        Document doc = new Document(getMyDir() + "VBA project.docm");
        Document destDoc = new Document();
        
        // Clone VbaProject to the document
        VbaProject copyVbaProject = doc.getVbaProject().deepClone();
        destDoc.setVbaProject(copyVbaProject);
        
        // In destination document we already have "Module1", because he was cloned with VbaProject
        // Therefore need to remove it before cloning
        VbaModule oldVbaModule = destDoc.getVbaProject().getModules().get("Module1");
        VbaModule copyVbaModule = doc.getVbaProject().getModules().get("Module1").deepClone();
        destDoc.getVbaProject().getModules().remove(oldVbaModule);
        destDoc.getVbaProject().getModules().add(copyVbaModule);
        
        destDoc.save(getArtifactsDir() + "Document.CloneVbaProject.docm");