windowsforms-405269-controls-and-libraries-rich-text-editor-rich-edit-control-document-macros-vba.md
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 WinForms allows you to obtain and remove VBA macro project associated with a document. VBA macros are available in the following document formats:
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); }
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
Use one of the following methods to remove VBA modules from the document:
VbaModuleCollection.Remove – removes a specific module from the collection.
VbaModuleCollection.Clear – clears the module collection.
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();
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