Back to Devexpress

VBA Macros in Rich Text Editor for WPF

wpf-405271-controls-and-libraries-rich-text-editor-rich-edit-control-document-vba-macros.md

latest3.2 KB
Original Source

VBA Macros in Rich Text Editor for WPF

  • Nov 29, 2024
  • 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:

csharp
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); }
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Dim document As Document = richEditControl.Document
Dim names = New List(Of String)()
If document.VbaProject.Modules.Count > 0 Then
    For Each [module] As VbaModule In document.VbaProject.Modules
        names.Add([module].Name)
    Next [module]
End If

Remove Macro Modules

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

The following code snippet removes all VBA modules:

csharp
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();
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

' Check if the current document has macros and remove them
Dim document As Document = richEditControl.Document

If document.VbaProject.Modules.Count > 0 Then
    document.VbaProject.Modules.Clear()
End If