DocumentBuilderInsertField Method (String)
Inserts a Word field into a document and updates the field result.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public Field InsertField(
	string fieldCode
)

Parameters

fieldCode
Type: SystemString
The field code to insert (without curly braces).

Return Value

Type: Field
A Field object that represents the inserted field.
Remarks

This method inserts a field into a document and updates the field result immediately. Aspose.Words can update fields of most types, but not all. For more details see the InsertField(String, String) overload.

Examples
Shows how to insert merge fields and move between them.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"MERGEFIELD MyMergeField1 \* MERGEFORMAT");
builder.InsertField(@"MERGEFIELD MyMergeField2 \* MERGEFORMAT");

Assert.AreEqual(2, doc.Range.Fields.Count);

// The second merge field starts immediately after the end of the first
// We'll move the builder's cursor to the end of the first so we can split them by text
builder.MoveToMergeField("MyMergeField1", true, false);

builder.Write(" Text between our two merge fields. ");

doc.Save(ArtifactsDir + "DocumentBuilder.MergeFields.docx");
Examples
Inserts a field into a document using DocumentBuilder and FieldCode.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a simple Date field into the document.
// When we insert a field through the DocumentBuilder class we can get the
// special Field object which contains information about the field.
Field dateField = builder.InsertField(@"DATE \* MERGEFORMAT");

// Update this particular field in the document so we can get the FieldResult.
dateField.Update();

// Display some information from this field.
// The field result is where the last evaluated value is stored. This is what is displayed in the document
// When field codes are not showing.
Console.WriteLine("FieldResult: {0}", dateField.Result);

// Display the field code which defines the behavior of the field. This can been seen in Microsoft Word by pressing ALT+F9.
Console.WriteLine("FieldCode: {0}", dateField.GetFieldCode());

// The field type defines what type of field in the Document this is. In this case the type is "FieldDate" 
Console.WriteLine("FieldType: {0}", dateField.Type);

// Finally let's completely remove the field from the document. This can easily be done by invoking the Remove method on the object.
dateField.Remove();
See Also