VBA macros are used to perform repetitive tasks, generate charts and forms, etc. in PowerPoint presentations. While working with the presentations programmatically, you may need to manipulate VBA macros. In accordance with that, in this article, you will learn how to add, extract or remove VBA macros in PowerPoint presentations using Java.
- Java API for PowerPoint VBA Macros
- Add VBA Macros in PowerPoint Presentations
- Extract VBA Macros from PowerPoint Presentations
- Remove VBA Macros from PowerPoint Presentations
TIP
You may want to try Aspose online VBA macro removal tool.
Java API for PowerPoint VBA Macros
In order to work with VBA macros in presentations, we will use Aspose.Slides for Java. It is a feature-rich API that lets you create or manipulate PowerPoint presentations from within your Java applications. You can either download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.7</version>
<classifier>jdk16</classifier>
</dependency>
Add VBA Macro in PowerPoint Presentations using Java
The following are the steps to add a VBA macro in a PowerPoint presentation using Java.
- First, create an instance of the Presentation class to load the PowerPoint presentation.
- Assign a new VbaProject using Presentation.setVbaProject() method.
- Add an empty VBA module using Presentation.getVbaProject().getModules().addEmptyModule(String) method.
- Get the reference of the added module into an IVbaModule object.
- Add source code to the VBA module using IVbaModule.setSourceCode() method.
- Create references to Microsoft Office and add them to Presentation.getVbaProject().getReferences() collection.
- Finally, save the presentation file using Presentation.save(String, SaveFormat.Pptm) method.
The following code sample shows how to add VBA macro in a PowerPoint presentation using Java.
Extract VBA Macros from PowerPoint using Java
The following are the steps to extract VBA macros from a PowerPoint presentation using Java.
- First, load the macro-enabled PowerPoint presentation using the Presentation class.
- Then, check if Presentation.getVbaProject() returns null.
- Loop through each IVbaModule in collection using Presentation.getVbaProject().getModules().
- Finally, extract source code using IVbaModule.getSourceCode() method.
The following code sample shows how to extract PowerPoint VBA macros.
Remove PowerPoint VBA Macros
You can also remove a particular VBA macro from a PowerPoint using its index in the VBA project. The following are the steps to perform this operation.
- First, load the macro-enabled PowerPoint presentation using the Presentation class.
- Then, remove VBA module by index using Presentation.getVbaProject().getModules().remove(IVbaModule) method.
- Finally, save the updated presentation using Presentation.save(String, SaveFormat.Pptm) method.
The following code sample shows how to remove a PowerPoint VBA macro.
Get a Free API License
You can use Aspose.Slides for Java without evaluation limitations by requesting a temporary license.
TIP
You may want to try Aspose online VBA macro removal tool.
Conclusion
In this article, you have learned how to work with PowerPoint VBA macros using Java. Particularly, you have seen how to add, extract and remove VBA macros in PowerPoint presentations. In order to explore other features of the API, you can consult the documentation. Moreover, you can feel free to let us know about your queries via our forum.