com.aspose.words
Class VbaProject

java.lang.Object
    extended by com.aspose.words.VbaProject
All Implemented Interfaces:
java.lang.Cloneable

public class VbaProject 
extends java.lang.Object

Provides access to VBA project information. A VBA project inside the document is defined as a collection of VBA modules.

Example:

Shows how to access a document's VBA project information.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project contains 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 access VBA modules in the collection either by index or by name.
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove a module from the collection.
vbaModules.remove(vbaModules.get(2));

Constructor Summary
VbaProject()
           Creates a blank VbaProject.
 
Property Getters/Setters Summary
intgetCodePage()
           Returns the VBA project’s code page.
booleanisSigned()
           Shows whether the VbaProject is signed or not.
VbaModuleCollectiongetModules()
           Returns collection of VBA project modules.
java.lang.StringgetName()
voidsetName(java.lang.String value)
           Gets or sets VBA project name.
VbaReferenceCollectiongetReferences()
           Gets a collection of VBA project references.
 
Method Summary
VbaProjectdeepClone()
           Performs a copy of the VbaProject.
 

Constructor Detail

VbaProject

public VbaProject()
Creates a blank VbaProject.

Example:

Shows how to create a VBA project 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");
module.setType(VbaModuleType.PROCEDURAL_MODULE);
module.setSourceCode("New source code");

// Add the module to the VBA project.
doc.getVbaProject().getModules().add(module);

doc.save(getArtifactsDir() + "VbaProject.CreateVBAMacros.docm");

Property Getters/Setters Detail

getCodePage

public int getCodePage()
Returns the VBA project’s code page.

Example:

Shows how to access a document's VBA project information.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project contains 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 access VBA modules in the collection either by index or by name.
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove a module from the collection.
vbaModules.remove(vbaModules.get(2));

isSigned

public boolean isSigned()
Shows whether the VbaProject is signed or not.

Example:

Shows how to access a document's VBA project information.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project contains 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 access VBA modules in the collection either by index or by name.
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove a module from the collection.
vbaModules.remove(vbaModules.get(2));

getModules

public VbaModuleCollection getModules()
Returns collection of VBA project modules.

Example:

Shows how to access a document's VBA project information.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project contains 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 access VBA modules in the collection either by index or by name.
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove a module from the collection.
vbaModules.remove(vbaModules.get(2));

getName/setName

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

Example:

Shows how to create a VBA project 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");
module.setType(VbaModuleType.PROCEDURAL_MODULE);
module.setSourceCode("New source code");

// Add the module to the VBA project.
doc.getVbaProject().getModules().add(module);

doc.save(getArtifactsDir() + "VbaProject.CreateVBAMacros.docm");

Example:

Shows how to access a document's VBA project information.
Document doc = new Document(getMyDir() + "VBA project.docm");

// A VBA project contains 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 access VBA modules in the collection either by index or by name.
vbaModules.get(0).setSourceCode("Your VBA code...");
vbaModules.get("Module1").setSourceCode("Your VBA code...");

// Remove a module from the collection.
vbaModules.remove(vbaModules.get(2));

getReferences

public VbaReferenceCollection getReferences()
Gets a collection of VBA project references.

Method Detail

deepClone

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

Example:

Shows how to deep clone a VBA project and module.
Document doc = new Document(getMyDir() + "VBA project.docm");
Document destDoc = new Document();

VbaProject copyVbaProject = doc.getVbaProject().deepClone();
destDoc.setVbaProject(copyVbaProject);

// In the destination document, we already have a module named "Module1"
// because we cloned it along with the project. We will need to remove the module.
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() + "VbaProject.CloneVbaProject.docm");

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.