VbaModule

VbaModule class

Provides access to VBA project module.

public class VbaModule

Constructors

Name Description
VbaModule() Creates an empty module.

Properties

Name Description
Name { get; set; } Gets or sets VBA project module name.
SourceCode { get; set; } Gets or sets VBA project module source code.
Type { get; set; } Specifies whether the module is a procedural module, document module, class module, or designer module.

Methods

Name Description
Clone() Performs a copy of the VbaModule.

Examples

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

Document doc = new Document(MyDir + "VBA project.docm");

// A VBA project contains a collection of VBA modules.
VbaProject vbaProject = doc.VbaProject;
    ? $"Project name: {vbaProject.Name} signed; Project code page: {vbaProject.CodePage}; Modules count: {vbaProject.Modules.Count()}\n"
    : $"Project name: {vbaProject.Name} not signed; Project code page: {vbaProject.CodePage}; Modules count: {vbaProject.Modules.Count()}\n");

VbaModuleCollection vbaModules = doc.VbaProject.Modules; 

Assert.AreEqual(vbaModules.Count(), 3);

foreach (VbaModule module in vbaModules)
    Console.WriteLine($"Module name: {module.Name};\nModule code:\n{module.SourceCode}\n");

// Set new source code for VBA module. You can access VBA modules in the collection either by index or by name.
vbaModules[0].SourceCode = "Your VBA code...";
vbaModules["Module1"].SourceCode = "Your VBA code...";

// Remove a module from the collection.
vbaModules.Remove(vbaModules[2]);

See Also