Aspose::Words::Vba::VbaModule class

VbaModule class

Provides access to VBA project module. To learn more, visit the Working with VBA Macros documentation article.

class VbaModule : public System::Object


Clone()Performs a copy of the VbaModule.
get_Name() constGets or sets VBA project module name.
get_SourceCode() constGets or sets VBA project module source code.
get_Type() constSpecifies whether the module is a procedural module, document module, class module, or designer module.
GetType() const override
Is(const System::TypeInfo&) const override
set_Name(const System::String&)Setter for Aspose::Words::Vba::VbaModule::get_Name.
set_SourceCode(const System::String&)Setter for Aspose::Words::Vba::VbaModule::get_SourceCode.
set_Type(Aspose::Words::Vba::VbaModuleType)Setter for Aspose::Words::Vba::VbaModule::get_Type.
static Type()
VbaModule()Creates an empty module.


Shows how to access a document’s VBA project information.

auto doc = MakeObject<Document>(MyDir + u"VBA project.docm");

// A VBA project contains a collection of VBA modules.
SharedPtr<VbaProject> vbaProject = doc->get_VbaProject();
std::cout << (vbaProject->get_IsSigned() ? String::Format(u"Project name: {0} signed; Project code page: {1}; Modules count: {2}\n",
                                                          vbaProject->get_Name(), vbaProject->get_CodePage(), vbaProject->get_Modules()->LINQ_Count())
                                         : String::Format(u"Project name: {0} not signed; Project code page: {1}; Modules count: {2}\n",
                                                          vbaProject->get_Name(), vbaProject->get_CodePage(), vbaProject->get_Modules()->LINQ_Count()))
          << std::endl;

SharedPtr<VbaModuleCollection> vbaModules = doc->get_VbaProject()->get_Modules();

ASSERT_EQ(vbaModules->LINQ_Count(), 3);

for (const auto& module_ : vbaModules)
    std::cout << "Module name: " << module_->get_Name() << ";\nModule code:\n" << module_->get_SourceCode() << "\n" << std::endl;

// Set new source code for VBA module. You can access VBA modules in the collection either by index or by name.
vbaModules->idx_get(0)->set_SourceCode(u"Your VBA code...");
vbaModules->idx_get(u"Module1")->set_SourceCode(u"Your VBA code...");

// Remove a module from the collection.

See Also