Working with VBA Macros

Visual Basic for Applications (VBA) for Microsoft Word is a simple but powerful programming language that can be used to extend the functionality. Aspose.Words API provides three classes to get access to the VBA project source code:

  • The VBAProject class provides access to VBA project information
  • The VBAModuleCollection class returns the collection of VBA project modules
  • The VbaModule class provides access to the VBA project module
  • The VbaModuleType enumeration defines the types of a model in a VBA project. The module can be a procedural module, document module, class module, or designer module

Create a VBA Project

Aspose.Words API provides the VbaProject property to get or set VbaProject in the document.

The following code example demonstrates how to create a VBA project and VBA Module along with basic properties e.g. Name and Type:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
VbaProject project = new VbaProject();
project.Name = "AsposeProject";
doc.VbaProject = project;
// Create a new module and specify a macro source code.
VbaModule module = new VbaModule();
module.Name = "AsposeModule";
module.Type = VbaModuleType.ProceduralModule;
module.SourceCode = "New source code";
// Add module to the VBA project.
doc.VbaProject.Modules.Add(module);
doc.Save(ArtifactsDir + "WorkingWithVba.CreateVbaProject.docm");

Read Macros

Aspose.Words also provides users with the ability to read VBA macros.

The following code example shows how to read VBA Macros from the document:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "VBA project.docm");
if (doc.VbaProject != null)
{
foreach (VbaModule module in doc.VbaProject.Modules)
{
Console.WriteLine(module.SourceCode);
}
}

Write or Modify Macros

Using Aspose.Words, users can modify VBA macros.

The following code example shows how to modify VBA Macros using the SourceCode property:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "VBA project.docm");
VbaProject project = doc.VbaProject;
const string newSourceCode = "Test change source code";
project.Modules[0].SourceCode = newSourceCode;

Clone VBA Project

With Aspose.Words it is also possible to clone VBA projects.

The following code example shows how to clone the VBA Project using the Clone property which creates a copy of the existing project:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "VBA project.docm");
Document destDoc = new Document { VbaProject = doc.VbaProject.Clone() };
destDoc.Save(ArtifactsDir + "WorkingWithVba.CloneVbaProject.docm");

Clone VBA Module

You can also clone VBA modules if needed.

The following code example shows how to clone the VBA Module using the Clone property which creates a copy of the existing project:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "VBA project.docm");
Document destDoc = new Document { VbaProject = new VbaProject() };
VbaModule copyModule = doc.VbaProject.Modules["Module1"].Clone();
destDoc.VbaProject.Modules.Add(copyModule);
destDoc.Save(ArtifactsDir + "WorkingWithVba.CloneVbaModule.docm");

Work with the VBA Project References

Aspose.Words API provides the VbaReferenceCollection class to work with VBA Project References representing a collection of VBA project references.

The following code example shows how to remove some references from the collection of references from a VBA project:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "VBA project.docm");
// Find and remove the reference with some LibId path.
const string brokenPath = "brokenPath.dll";
VbaReferenceCollection references = doc.VbaProject.References;
for (int i = references.Count - 1; i >= 0; i--)
{
VbaReference reference = doc.VbaProject.References.ElementAt(i);
string path = GetLibIdPath(reference);
if (path == brokenPath)
references.RemoveAt(i);
}
doc.Save(ArtifactsDir + "WorkingWithVba.RemoveVbaReferences.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Returns string representing LibId path of a specified reference.
/// </summary>
private string GetLibIdPath(VbaReference reference)
{
switch (reference.Type)
{
case VbaReferenceType.Registered:
case VbaReferenceType.Original:
case VbaReferenceType.Control:
return GetLibIdReferencePath(reference.LibId);
case VbaReferenceType.Project:
return GetLibIdProjectPath(reference.LibId);
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Returns path from a specified identifier of an Automation type library.
/// </summary>
/// <remarks>
/// Please see details for the syntax at [MS-OVBA], 2.1.1.8 LibidReference.
/// </remarks>
private string GetLibIdReferencePath(string libIdReference)
{
if (libIdReference != null)
{
string[] refParts = libIdReference.Split('#');
if (refParts.Length > 3)
return refParts[3];
}
return "";
}
/// <summary>
/// Returns path from a specified identifier of an Automation type library.
/// </summary>
/// <remarks>
/// Please see details for the syntax at [MS-OVBA], 2.1.1.12 ProjectReference.
/// </remarks>
private string GetLibIdProjectPath(string libIdProject)
{
return (libIdProject != null) ? libIdProject.Substring(3) : "";
}