Presentation via VBA
Note
When you convert a presentation containing macros to a different file format (PDF, HTML, etc.), Aspose.Slides ignores all macros (macros are not carried into the resulting file).
When you add macros to a presentation or resave a presentation containing macros, Aspose.Slides simply writes the bytes for the macros.
Aspose.Slides never runs the macros in a presentation.
Add VBA Macros
Aspose.Slides provides the VbaProject class to allow you to create VBA projects (and project references) and edit existing modules. You can use the VbaProject class to manage VBA embedded in a presentation.
- Create an instance of the Presentation class.
- Use the VbaProject constructor to add a new VBA project.
- Add a module to the VbaProject.
- Set the module source code.
- Add references to
. - Add references to Microsoft Office.
- Associate the references with the VBA project.
- Save the presentation.
This JavaScript code shows you how to add a VBA macro from scratch to a presentation:
// Creates an instance of the presentation class
var pres = new aspose.slides.Presentation();
try {
// Creates a new VBA Project
pres.setVbaProject(new aspose.slides.VbaProject());
// Adds an empty module to the VBA project
var module = pres.getVbaProject().getModules().addEmptyModule("Module");
// Sets the module source code
module.setSourceCode("Sub Test(oShape As Shape)MsgBox Test End Sub");
// Creates a reference to <stdole>
var stdoleReference = new aspose.slides.VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");
// Creates a reference to Office
var officeReference = new aspose.slides.VbaReferenceOleTypeLib("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");
// Adds references to the VBA project
pres.getVbaProject().getReferences().add(stdoleReference);
pres.getVbaProject().getReferences().add(officeReference);
// Saves the Presentation
pres.save("test.pptm", aspose.slides.SaveFormat.Pptm);
} finally {
if (pres != null) {
pres.dispose();
}
}
Remove VBA Macros
Using the VbaProject property under the Presentation class, you can remove a VBA macro.
- Create an instance of the Presentation class and load the presentation containing the macro.
- Access the Macro module and remove it.
- Save the modified presentation.
This JavaScript code shows you how to remove a VBA macro:
// Loads the presentation containing the macro
var pres = new aspose.slides.Presentation("VBA.pptm");
try {
// Accesses the Vba module and removes it
pres.getVbaProject().getModules().remove(pres.getVbaProject().getModules().get_Item(0));
// Saves the Presentation
pres.save("test.pptm", aspose.slides.SaveFormat.Pptm);
} finally {
if (pres != null) {
pres.dispose();
}
}
Extract VBA Macros
- Create an instance of the Presentation class and load the presentation containing the macro.
- Check if the presentation contains a VBA Project.
- Loop through all the modules contained in the VBA Project to view the macros.
This JavaScript code shows you how to extract VBA macros from a presentation containing macros:
// Loads the presentation containing the macro
var pres = new aspose.slides.Presentation("VBA.pptm");
try {
// Checks whether the Presentation contains a VBA Project
if (pres.getVbaProject() != null) {
for (let i = 0; i < pres.getVbaProject().getModules().size(); i++) {
let module = pres.getVbaProject().getModules().get_Item(i);
console.log(module.getName());
console.log(module.getSourceCode());
}
}
} finally {
if (pres != null) {
pres.dispose();
}
}