Skip to main content
All docs
V25.1
  • VBA Macros in Rich Text Editor for WPF

    • 2 minutes to read

    VBA (Visual Basic for Applications) macros are scripts written in Microsoft’s Visual Basic for Applications language. These scripts can automate tasks within a document (for example, to format or insert text).

    Rich Text Editor for WPF allows you to obtain and remove VBA macro project associated with a document. VBA macros are available in the following document formats:

    • DOCM
    • DOTM
    • DOC

    Obtain VBA Macro Project

    The Document.VbaProject property retrieves the VBA project associated with a document. Use the VbaProject.Modules property to get a collection of macro modules. The VbaModuleCollection.Item[String] property allows you to obtain an individual VbaModule by its name.

    You can get the module name and binary data. The module name starts with a VBA\ prefix. The binary data contains all macros of a module. You can parse binary data manually to retrieve an individual macro.

    The following code snippet retrieves VBA modules and collects all module names to a separate list:

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    
    Document document = richEditControl.Document;
    var names = new List<string>();
    if (document.VbaProject.Modules.Count > 0)
        foreach (VbaModule module in document.VbaProject.Modules)
        { names.Add(module.Name); }
    

    Remove Macro Modules

    Use one of the following methods to remove VBA modules from the document:

    The following code snippet removes all VBA modules:

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    
    // Check if the current document has macros and remove them
    Document document = richEditControl.Document;
    
    if (document.VbaProject.Modules.Count > 0)
        document.VbaProject.Modules.Clear();