The VBA macros are used to automate different functionalities in PowerPoint presentations. For example, you can use VBA to perform repetitive tasks, generate charts and forms, etc. In this article, you will learn how to work with PowerPoint VBA macros programmatically. Particularly, the article will cover how to add, extract or remove VBA macros in PowerPoint presentations using C#.
- C# API for PowerPoint VBA Macros
- Add VBA Macros in PowerPoint Presentations using C#
- Extract VBA Macros from PowerPoint Presentations
- Remove VBA Macros from PowerPoint Presentations
- Get a Free API License
C# API for PowerPoint VBA Macros
Aspose.Slides for .NET is a feature-rich API that lets you create, edit and convert PowerPoint presentations using C#. Furthermore, the API allows you to work with VBA macros seamlessly. In order to use the API, you can either download its DLL or install it using NuGet.
PM> Install-Package Aspose.Slides.NET
Add VBA Macro in PowerPoint Presentations using C#
The following are the steps to add a VBA macro in a PowerPoint presentation using C#.
- First, create an instance of the Presentation class to load the PowerPoint presentation.
- Assign a new VbaProject to Presentation.VbaProject property.
- Add an empty VBA module using Presentation.VbaProject.Modules.AddEmptyModule(string) method.
- Get the reference of the added module into an IVbaModule object.
- Add source code to the VBA module using IVbaModule.SourceCode property.
- Create references to Microsoft Office and add them to Presentation.VbaProject.References 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 C#.
Extract VBA Macros from PowerPoint using C#
Aspose.Slides for .NET also allows you to extract VBA modules from VBA projects in PowerPoint presentations. The following are the steps to perform this operation.
- First, load the macro-enabled PowerPoint presentation using the Presentation class.
- Check if Presentation.VbaProject property is not null.
- Loop through each IVbaModule in Presentation.VbaProject.Modules collection.
- Finally, extract source code using IVbaModule.SourceCode property.
The following code sample shows how to extract PowerPoint VBA macros using C#.
Remove PowerPoint VBA Macros
You can also remove a particular VBA macro from a PowerPoint presentation. For this, you will access and remove the VBA module by its index from the VBA project. The following are the steps to perform this operation.
- First, load the macro-enabled PowerPoint presentation using the Presentation class.
- Remove VBA module by index using Presentation.VbaProject.Modules.Remove(Presentation.VbaProject.Modules[0]) 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 try Aspose.Slides for .NET without evaluation limitations by requesting a temporary license.
Conclusion
In this article, you have learned how to work with PowerPoint VBA macros using C#. 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.
See Also
Tip: You may want to check out Aspose FREE macro removal web app.