Editing VBA Source Code

Overview

Microsoft Project (MPP/XML) files may contain VBA modules that store macros to automate project-related tasks. Starting from version 25.6, Aspose.Tasks for .NET allows not only reading but also editing the source code of existing VBA modules in a project.

This makes it possible to maintain and update automation logic without relying on Microsoft Project.

Editing Source Code of an Existing Module

The following example demonstrates how to edit the source code of a module named Module1 in an existing project.

 1    Project project = new Project("FileWithVbaProject.mpp");
 2
 3    if (project.VbaProject.Modules.Count == 0)
 4    {
 5        throw new InvalidOperationException("Project should contain VBA modules");
 6    }
 7
 8    var existingModule = project.VbaProject.Modules["Module1"];
 9    existingModule.SourceCode = @"Sub Method()
10    MsgBox ""This is an updated text.""
11    End Sub";
12            
13    // WriteVba flag should be specified in order to apply changes to MPP file.
14    project.Save("output.mpp", new MPPSaveOptions { WriteVba = true });

This example checks if the project contains VBA modules, finds a module by its name, and replaces its source code. To apply changes, the WriteVba flag must be enabled when saving the project.

FAQ

Q: Can I update multiple modules at once?

Q: What happens if I try to update a non-existent module?

Q: Can I edit VBA source code in a project without VBA modules?

Q: Can I rename VBA modules?

Conclusion

Editing VBA source code with Aspose.Tasks for .NET gives full control over project macros, enabling developers to automate, adjust, and maintain custom logic inside Microsoft Project files without relying on Microsoft Project itself.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.