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?
- Yes. You can iterate through the
VbaProject.Modules
collection and update each module’sSourceCode
property.
Q: What happens if I try to update a non-existent module?
- An exception will be thrown. You must check if the module exists before editing.
Q: Can I edit VBA source code in a project without VBA modules?
- No. The target Microsoft Project file must already contain a VBA project with at least one module.
Q: Can I rename VBA modules?
- Currently, only updating the source code of existing modules and adding new modules is supported.
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.