VbaModuleCollection

VbaModuleCollection class

Represents a collection of VbaModule objects.

To learn more, visit the Working with VBA Macros documentation article.

public sealed class VbaModuleCollection : IEnumerable<VbaModule>

Properties

NameDescription
Count { get; }Returns the number of VBA modules in the collection.
Item { get; }Retrieves a VbaModule object by index. (2 indexers)

Methods

NameDescription
Add(VbaModule)Adds a module to the collection.
Remove(VbaModule)Removes the specified module from the collection.

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;
Console.WriteLine(vbaProject.IsSigned
    ? $"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