# Manage VBA Projects in Presentations in .NET

> Discover how to generate and manipulate PowerPoint and OpenDocument presentations via VBA with Aspose.Slides for .NET to streamline your workflow.

Source: https://docs.aspose.com/slides/net/presentation-via-vba/
Updated: 2026-08-13


## **Introduction**

The [Aspose.Slides.Vba](https://reference.aspose.com/slides/net/aspose.slides.vba/) namespace contains classes and interfaces for working with macros and VBA code.

{{% alert title="Note" color="warning" %}} 

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.

{{% /alert %}}

## **Add VBA Macros**

Aspose.Slides provides the [VbaProject](https://reference.aspose.com/slides/net/aspose.slides.vba/vbaproject/) class to allow you to create VBA projects (and project references) and edit existing modules. You can use the [IVbaProject](https://reference.aspose.com/slides/net/aspose.slides.vba/ivbaproject/) interface to manage VBA embedded in a presentation.

1. Create an instance of the [Presentation](https://reference.aspose.com/slides/net/aspose.slides/presentation/) class.
1. Use the [VbaProject](https://reference.aspose.com/slides/net/aspose.slides.vba/vbaproject/vbaproject/#constructor) constructor to add a new VBA project.
1. Add a module to the VbaProject.
1. Set the module source code.
1. Add references to <stdole>.
1. Add references to **Microsoft Office**.
1. Associate the references with the VBA project.
1. Save the presentation.

This C# code shows you how to add a VBA macro from scratch to a presentation:

```c#
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.Vba;

// Creates an instance of the presentation class
using (Presentation presentation = new Presentation())
{
    // Creates a new VBA Project
    presentation.VbaProject = new VbaProject();

    // Adds an empty module to the VBA project
    IVbaModule module = presentation.VbaProject.Modules.AddEmptyModule("Module");

    // Sets the module source code
    module.SourceCode = @"Sub Test(oShape As Shape) MsgBox ""Test"" End Sub";

    // Creates a reference to <stdole>
    VbaReferenceOleTypeLib stdoleReference =
        new VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");

    // Creates a 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");

    // Adds references to the VBA project
    presentation.VbaProject.References.Add(stdoleReference);
    presentation.VbaProject.References.Add(officeReference);

    // Saves the Presentation
    presentation.Save("AddVBAMacros_out.pptm", SaveFormat.Pptm);
}
```

{{% alert color="info" %}} 

You may want to check out **Aspose** [Macro Remover](https://products.aspose.app/slides/remove-macros), which a free web app used to remove macros from PowerPoint, Excel, and Word documents. 

{{% /alert %}} 

## **Remove VBA Macros**
Using the [VbaProject](https://reference.aspose.com/slides/net/aspose.slides/presentation/vbaproject/) property under the [Presentation](https://reference.aspose.com/slides/net/aspose.slides/presentation/) class, you can remove a VBA macro.

1. Create an instance of the [Presentation](https://reference.aspose.com/slides/net/aspose.slides/presentation/) class and load the presentation containing the macro.
1. Access the Macro module and remove it.
1. Save the modified presentation.

This C# code shows you how to remove a VBA macro:

```c#
using Aspose.Slides;
using Aspose.Slides.Export;

// Loads the presentation containing the macro
using (Presentation presentation = new Presentation("VBA.pptm"))
{
    // Accesses the Vba module and removes it
    presentation.VbaProject.Modules.Remove(presentation.VbaProject.Modules[0]);

    // Saves the Presentation
    presentation.Save("RemovedVBAMacros_out.pptm", SaveFormat.Pptm);
}
```


## **Extract VBA Macros**
1. Create an instance of the [Presentation](https://reference.aspose.com/slides/net/aspose.slides/presentation/) class and load the presentation containing the macro.
2. Check if the presentation contains a VBA Project.
3. Loop through all the modules contained in the VBA Project to view the macros.

This C# code shows you how to extract VBA macros from a presentation containing macros:

```c#
using Aspose.Slides;
using Aspose.Slides.Vba;

    // Loads the presentation containing the macro
using (Presentation pres = new Presentation("VBA.pptm"))
{
	if (pres.VbaProject != null) // Checks whether the Presentation contains a VBA Project
	{
		foreach (IVbaModule module in pres.VbaProject.Modules)
		{
			Console.WriteLine(module.Name);
			Console.WriteLine(module.SourceCode);
		}
	}
}
```

## **Check Whether a VBA Project Is Password-Protected**

Using the [IVbaProject.IsPasswordProtected](https://reference.aspose.com/slides/net/aspose.slides.vba/ivbaproject/ispasswordprotected/) property, you can determine whether a project’s properties are password-protected.

1. Create an instance of the [Presentation](https://reference.aspose.com/slides/net/aspose.slides/presentation/) class and load a presentation that contains a macro.
2. Check whether the presentation contains a [VBA project](https://reference.aspose.com/slides/net/aspose.slides.vba/vbaproject/).
3. Check whether the VBA project is password-protected to view its properties.

```cs
using Aspose.Slides;

using (Presentation presentation = new Presentation("VBA.pptm"))
{
    if (presentation.VbaProject != null) // Check whether the presentation contains a VBA project.
    {
        if (presentation.VbaProject.IsPasswordProtected)
        {
            Console.WriteLine($"The VBA Project '{presentation.VbaProject.Name}' is protected by password to view project properties.");
        }
    }
}
```

## **FAQ**

### What happens to macros if I save the presentation as PPTX?

Macros will be removed because PPTX does not support VBA. To keep macros, choose PPTM, PPSM, or POTM.

### Can Aspose.Slides run macros inside a presentation to, for example, refresh data?

No. The library never executes VBA code; execution is only possible inside PowerPoint with the appropriate security settings.

### Is working with ActiveX controls linked to VBA code supported?

Yes, you can access existing [ActiveX controls](/slides/net/activex/), modify their properties, and remove them. This is useful when macros interact with ActiveX.



## Related articles

- [VBA Macro](https://docs.aspose.com/slides/net/examples/elements/vba-macro/)
- [Public API and Backwards Incompatible Changes in Aspose.Slides for .NET 14.8.0](https://docs.aspose.com/slides/net/public-api-and-backwards-incompatible-changes-in-aspose-slides-for-net-14-8-0/)