VBA Macro

This article demonstrates how to add, access, and remove VBA macros using Aspose.Slides for .NET.

The checks verify VBA source and macro-enabled presentation storage. Executing VBA requires a compatible application and its macro security permissions.

Copy a C# block into Program.cs in a .NET project with Aspose.Slides.NET6.CrossPlatform installed. Each block is a complete program. Run one block at a time. The ZIP contains the IDE-ready project and its verification checks. The copied code runs in evaluation mode. For licensed output, apply your license before creating a presentation, as described in the licensing guide.

Download and run

Download the complete project, extract it, and open Example.sln or run:

dotnet restore Example.sln
dotnet run --project Example.csproj -- --mode evaluation

The download includes every case below, its fixture helpers and inputs. Program.cs runs all cases; use --action block-001 to run one independently. Outputs and an action report are written to the printed output directory. No previous example or working-directory setup is required. Evaluation adds a watermark and can truncate read-back text; the short fixtures still have explicit structural checks. For full text and no watermark, use --mode licensed --license PATH with your own valid license. See the setup guide for supported SDKs and native prerequisites.

Add a VBA Macro

Create a presentation with a VBA project and a simple macro module.

using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.Vba;

using var presentation = new Presentation();
presentation.VbaProject = new VbaProject();

var module = presentation.VbaProject.Modules.AddEmptyModule("Module");
module.SourceCode = """
    Sub Test()
        MsgBox "Hi"
    End Sub
    """;

presentation.Save("vba-macro-01.pptm", SaveFormat.Pptm);

Access a VBA Macro

Retrieve the first module from the VBA project.

using System;
using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.Vba;

using var presentation = new Presentation();
presentation.VbaProject = new VbaProject();

var module = presentation.VbaProject.Modules.AddEmptyModule("Module");
module.SourceCode = """
    Sub Test()
        MsgBox "Hi"
    End Sub
    """;

var firstModule = presentation.VbaProject.Modules[0];
Console.WriteLine($"Module: {firstModule.Name}");

presentation.Save("vba-macro-02.pptm", SaveFormat.Pptm);

Remove a VBA Macro

Delete a module from the VBA project.

using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.Vba;

using var presentation = new Presentation();
presentation.VbaProject = new VbaProject();

var module = presentation.VbaProject.Modules.AddEmptyModule("Module");
module.SourceCode = """
    Sub Test()
        MsgBox "Hi"
    End Sub
    """;

presentation.VbaProject.Modules.Remove(module);

presentation.Save("vba-macro-03.pptm", SaveFormat.Pptm);