FieldMacroButton

FieldMacroButton class

Implements the MACROBUTTON field.

To learn more, visit the Working with Fields documentation article.

public class FieldMacroButton : Field

Constructors

NameDescription
FieldMacroButton()The default constructor.

Properties

NameDescription
DisplayResult { get; }Gets the text that represents the displayed field result.
DisplayText { get; set; }Gets or sets the text to appear as the “button” that is selected to run the macro or command.
End { get; }Gets the node that represents the field end.
Format { get; }Gets a FieldFormat object that provides typed access to field’s formatting.
IsDirty { get; set; }Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
IsLocked { get; set; }Gets or sets whether the field is locked (should not recalculate its result).
LocaleId { get; set; }Gets or sets the LCID of the field.
MacroName { get; set; }Gets or sets the name of the macro or command to run.
Result { get; set; }Gets or sets text that is between the field separator and field end.
Separator { get; }Gets the node that represents the field separator. Can be null.
Start { get; }Gets the node that represents the start of the field.
virtual Type { get; }Gets the Microsoft Word field type.

Methods

NameDescription
GetFieldCode()Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
GetFieldCode(bool)Returns text between field start and field separator (or field end if there is no separator).
Remove()Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
Unlink()Performs the field unlink.
Update()Performs the field update. Throws if the field is being updated already.
Update(bool)Performs a field update. Throws if the field is being updated already.

Remarks

Allows a macro or command to be run.

In Aspose.Words this field can also act as a merge field.

Examples

Shows how to use MACROBUTTON fields to allow us to run a document’s macros by clicking.

Document doc = new Document(MyDir + "Macro.docm");
DocumentBuilder builder = new DocumentBuilder(doc);

Assert.IsTrue(doc.HasMacros);

// Insert a MACROBUTTON field, and reference one of the document's macros by name in the MacroName property.
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());

// Use the property to reference "ViewZoom200", a macro that ships with Microsoft Word.
// We can find all other macros via View -> Macros (dropdown) -> View Macros.
// In that menu, select "Word Commands" from the "Macros in:" drop down.
// If our document contains a custom macro with the same name as a stock macro,
// our macro will be the one that the MACROBUTTON field runs.
builder.InsertParagraph();
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