DocumentHasMacros Property

Returns true if the document has a VBA project (macros).

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.9.0


public bool HasMacros { get; }

Property Value

Type: Boolean


Shows how to use MACROBUTTON fields that enable us to run macros by clicking.
// Open a document that contains macros
Document doc = new Document(MyDir + "Macro.docm");
DocumentBuilder builder = new DocumentBuilder(doc);


// Insert a MACROBUTTON field and reference by name a macro that exists within the input document
FieldMacroButton field = (FieldMacroButton)builder.InsertField(FieldType.FieldMacroButton, true);
field.MacroName = "MyMacro";
field.DisplayText = "Double click to run macro: " + field.MacroName;

Assert.AreEqual(" MACROBUTTON  MyMacro Double click to run macro: MyMacro", field.GetFieldCode());

// Reference "ViewZoom200", a macro that was shipped with Microsoft Word, found under "Word commands"
// If our document has a macro of the same name as one from another source, the field will select ours to run
field = (FieldMacroButton)builder.InsertField(FieldType.FieldMacroButton, true);
field.MacroName = "ViewZoom200";
field.DisplayText = "Run " + field.MacroName;

Assert.AreEqual(" MACROBUTTON  ViewZoom200 Run ViewZoom200", field.GetFieldCode());

// Save the document as a macro-enabled document type
doc.Save(ArtifactsDir + "Field.MACROBUTTON.docm");

See Also