Working with VBA Macros

Overview

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

Creating a VBA Project

Aspose.Words API provides the Dcoument.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
Document doc = new Document();
// Create a new VBA project.
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(dataDir + "VbaProject_out.docm");

Read Macros

The following code example demonstrates 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
Document doc = new Document(dataDir + "Document.dot");
if (doc.VbaProject != null)
{
foreach (VbaModule module in doc.VbaProject.Modules)
{
Console.WriteLine(module.SourceCode);
}
}

Write or Modify Macros

The following code example demonstrates how to modify VBA Macros using the VbaModule.SourceCode property.

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

Clone VBA Project

The following code example demonstrates how to clone the VBA Project using the VbaProject.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
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
// Clone the whole project.
destDoc.VbaProject = doc.VbaProject.Clone();
destDoc.Save(dataDir + "output.docm");

Clone VBA Module

The following code example demonstrates how to clone the VBA Module using the VbaModule.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
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
destDoc.VbaProject = new VbaProject();
// Clone a single module.
VbaModule copyModule = doc.VbaProject.Modules["Module1"].Clone();
destDoc.VbaProject.Modules.Add(copyModule);
destDoc.Save(dataDir + "output.docm");

Working with the VBA Project References

Aspose.Words API provides VbaReferenceCollection class to work with VBA Project References representing a collection of VBA project references. The following code example demonstrates 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
Document doc = new Document(dataDir + "VbaProject.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(dataDir + "NoBrokenRef.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
/// <summary>
/// Returns string representing LibId path of a specified reference.
/// </summary>
private static 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 static 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 static string GetLibIdProjectPath(string libIdProject)
{
return (libIdProject != null) ? libIdProject.Substring(3) : "";
}