add remove powerpoint vba C#

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

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#.

The following code sample shows how to add VBA macro in a PowerPoint presentation using C#.

// Load presentation
using (Presentation presentation = new Presentation("presentation.pptm"))
{
// Create new VBA Project
presentation.VbaProject = new VbaProject();
// Add empty module to the VBA project
IVbaModule module = presentation.VbaProject.Modules.AddEmptyModule("Module");
// Set module's source code
module.SourceCode = @"Sub Test(oShape As Shape) MsgBox ""Test"" End Sub";
// Create reference to <stdole>
VbaReferenceOleTypeLib stdoleReference =
new VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");
// Create reference to Office
VbaReferenceOleTypeLib officeReference =
new 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");
// Add references to the VBA project
presentation.VbaProject.References.Add(stdoleReference);
presentation.VbaProject.References.Add(officeReference);
// Save Presentation
presentation.Save("AddVBAMacros.pptm", SaveFormat.Pptm);
}

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.

The following code sample shows how to extract PowerPoint VBA macros using C#.

// Load presentation
using (Presentation pres = new Presentation("presentation.pptm"))
{
if (pres.VbaProject != null) // check if Presentation contains VBA Project
{
foreach (IVbaModule module in pres.VbaProject.Modules)
{
Console.WriteLine(module.Name);
Console.WriteLine(module.SourceCode);
}
}
}

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.

The following code sample shows how to remove a PowerPoint VBA macro.

// Load presentation
using (Presentation presentation = new Presentation("Presentation.pptm"))
{
// Remove the VBA module
presentation.VbaProject.Modules.Remove(presentation.VbaProject.Modules[0]);
// Save presentation
presentation.Save("RemovedVBAMacros.pptm", SaveFormat.Pptm);
}

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.