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 VBAModulesCollection class returns the collection of VBA project modules
  • The VbaModule class provides access to the VBA project module

Create a VBA Project

Aspose.Words API provides Document.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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// Create a new VBA project.
System::SharedPtr<VbaProject> project = System::MakeObject<VbaProject>();
project->set_Name(u"AsposeProject");
doc->set_VbaProject(project);
// Create a new module and specify a macro source code.
System::SharedPtr<VbaModule> vbModule = System::MakeObject<VbaModule>();
vbModule->set_Name(u"AsposeModule");
vbModule->set_Type(VbaModuleType::ProceduralModule);
vbModule->set_SourceCode(u"New source code");
// Add module to the VBA project.
doc->get_VbaProject()->get_Modules()->Add(vbModule);
doc->Save(outputDataDir + u"WorkingWithVbaMacros.CreateVbaMacros.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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"VbaProject.docm");
if (doc->get_VbaProject() != nullptr)
{
for (System::SharedPtr<VbaModule> module : System::IterateOver(doc->get_VbaProject()->get_Modules()))
{
std::cout << module->get_SourceCode().ToUtf8String() << std::endl;
}
}

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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"VbaProject.docm");
System::SharedPtr<VbaProject> project = doc->get_VbaProject();
System::String newSourceCode = u"Test change source code";
// Choose a module, and set a new source code.
project->get_Modules()->idx_get(0)->set_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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"VbaProject.docm");
System::SharedPtr<VbaProject> project = doc->get_VbaProject();
System::SharedPtr<Document> destDoc = System::MakeObject<Document>();
// Clone the whole project.
destDoc->set_VbaProject(doc->get_VbaProject()->Clone());
destDoc->Save(outputDataDir + u"WorkingWithVbaMacros.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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"VbaProject.docm");
System::SharedPtr<VbaProject> project = doc->get_VbaProject();
System::SharedPtr<Document> destDoc = System::MakeObject<Document>();
destDoc->set_VbaProject(System::MakeObject<VbaProject>());
// Clone a single module.
System::SharedPtr<VbaModule> copyModule = doc->get_VbaProject()->get_Modules()->idx_get(u"AsposeModule")->Clone();
destDoc->get_VbaProject()->get_Modules()->Add(copyModule);
destDoc->Save(outputDataDir + u"WorkingWithVbaMacros.CloneVbaModule.docm");