VBA Macros in Word documents

Visual Basic for Applications is human-readable and editable programming code that gets generated when you record a macro. Today, it is widely-used with other Microsoft Office applications such as MS-Word, MS-Word, and MS-Access.

The VBA Project is where the modules and forms are stored when you write VBA code in Word.

In GemBox.Document vba project is represented by VbaProject element and can be loaded from and saved to macro-enabled (DOCM) file format only.

The following example shows how you can create an Word VBA module in C# and VB.NET.

Word VBA module created with GemBox.Document
Screenshot of Word VBA module created with GemBox.Document
using GemBox.Document;
using GemBox.Document.Vba;

class Program
{
    public static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        // Create the module.
        VbaModule vbaModule = document.VbaProject.Modules.Add("SampleModule", VbaModuleType.Document);
        vbaModule.Code = 
@"Sub WriteHello()
    Selection.TypeText Text:=""Hello World!""
End Sub";

        // Save the document as macro-enabled Word file.
        document.Save("AddVbaModule.docm");
    }
}
Imports GemBox.Document
Imports GemBox.Document.Vba

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim section As New Section(document)
        document.Sections.Add(section)

        ' Create the module.
        Dim vbaModule As VbaModule = document.VbaProject.Modules.Add("SampleModule", VbaModuleType.Document)
        vbaModule.Code =
"Sub WriteHello()
    Selection.TypeText Text:=""Hello World!""
End Sub"

        ' Save the document as macro-enabled Word file.
        document.Save("AddVbaModule.docm")
    End Sub
End Module

The following example shows how you can update an Word VBA module in C# and VB.NET.

Word VBA module updated with GemBox.Document
Screenshot of Word VBA module updated with GemBox.Document
using GemBox.Document;
using GemBox.Document.Vba;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%#SampleVba.docm%");

        // Get the module.
        VbaModule vbaModule = document.VbaProject.Modules["ThisDocument"];
        // Update text for the popup message.
        vbaModule.Code = vbaModule.Code.Replace("Hello world!", "Hello from GemBox.Document!");

        document.Save("UpdateVbaModule.docm");
    }
}
Imports GemBox.Document
Imports GemBox.Document.Vba

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%#SampleVba.docm%")

        ' Get the module.
        Dim vbaModule As VbaModule = document.VbaProject.Modules("ThisDocument")
        ' Update text for the popup message.
        vbaModule.Code = vbaModule.Code.Replace("Hello world!", "Hello from GemBox.Document!")

        document.Save("UpdateVbaModule.docm")
    End Sub
End Module

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy