Back to Devexpress

FieldCollection.Update() Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-fieldcollection.md

latest13.9 KB
Original Source

FieldCollection.Update() Method

Updates all fields in the collection.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
void Update()
vb
Sub Update

Remarks

Use the Field.Update method to update a field. The Document.UpdateAllFields method allows you to update all fields in the document.

Examples

The examples below demonstrate how to update all fields in specific parts of a document (main body, text box, header, footer, comment, footnote, and endnote).

Update Fields in the Main Document Body

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;
    //...
    // Update all fields in the main document body.
    document.Fields.Update();
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
  ' Access a document.
  Dim document As Document = wordProcessor.Document
  '...
  ' Update all fields in the main document body.
  document.Fields.Update()
End Using

Update Fields in Text Boxes

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;
    // Check all shapes in the document.
    foreach (Shape shape in document.Shapes)
    {
        // Check whether the shape is a text box.
        if (shape.ShapeFormat.HasText)
        {
            // Access text box content.
            SubDocument textBoxDocument = shape.ShapeFormat.TextBox.Document;
            //...
            // Update all fields in the text box.
            textBoxDocument.Fields.Update();
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
    ' Access a document.
    Dim document As Document = wordProcessor.Document
    ' Check all shapes in the document.
    For Each shape As Shape In document.Shapes
        ' Check whether the shape is a text box.
        If shape.ShapeFormat.HasText Then
            ' Access text box content.
            Dim textBoxDocument As SubDocument = shape.ShapeFormat.TextBox.Document
            '...
            ' Update all fields in the text box.
            textBoxDocument.Fields.Update()
        End If
    Next shape
End Using

Update Fields in Headers and Footers

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;
    // Check all document sections.
    foreach (Section section in document.Sections)
    {
        // Check whether the section has a primary header.
        if (section.HasHeader(HeaderFooterType.Primary))
        {
            // Start to edit the document header.
            SubDocument headerContent = section.BeginUpdateHeader();
            //...
            // Update all fields in the document header.
            headerContent.Fields.Update();
            // Finalize to edit the document header.
            section.EndUpdateHeader(headerContent);
        }

        // Check whether the section has a primary footer.
        if (section.HasFooter(HeaderFooterType.Primary))
        {
            // Start to edit the document footer.
            SubDocument footerContent = section.BeginUpdateFooter();
            //...
            // Update all fields in the document footer.
            footerContent.Fields.Update();
            // Finalize to edit the document footer.
            section.EndUpdateFooter(footerContent);
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
    ' Access a document.
    Dim document As Document = wordProcessor.Document
    ' Check all document sections.
    For Each section As Section In document.Sections
        ' Check whether the section has a primary header.
        If section.HasHeader(HeaderFooterType.Primary) Then
            ' Start to edit the document header.
            Dim headerContent As SubDocument = section.BeginUpdateHeader()
            '...
            ' Update all fields in the document header.
            headerContent.Fields.Update()
            ' Finalize to edit the document header.
            section.EndUpdateHeader(headerContent)
        End If

        ' Check whether the section has a primary footer.
        If section.HasFooter(HeaderFooterType.Primary) Then
            ' Start to edit the document footer.
            Dim footerContent As SubDocument = section.BeginUpdateFooter()
            '...
            ' Update all fields in the document footer.
            footerContent.Fields.Update()
            ' Finalize to edit the document footer.
            section.EndUpdateFooter(footerContent)
        End If
    Next section
End Using

Update Fields in Comments

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;
    // Check all document comments.
    foreach (Comment comment in document.Comments)
    {
        // Access comment content.
        SubDocument commentContent = comment.BeginUpdate();
        //...
        // Update all fields in the comment.
        commentContent.Fields.Update();
        // Finalize to edit the comment.
        comment.EndUpdate(commentContent);
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
  ' Access a document.
  Dim document As Document = wordProcessor.Document
  ' Check all document comments
  For Each comment As Comment In document.Comments
    ' Access comment content.
    Dim commentContent As SubDocument = comment.BeginUpdate()
    '...
    ' Update all fields in the comment.
    commentContent.Fields.Update()
    ' Finalize to edit the comment.
    comment.EndUpdate(commentContent)
  Next comment
End Using

Update Fields in Footnotes and Endnotes

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Access a document.
    Document document = wordProcessor.Document;
    // Check all document footnotes.
    foreach (Note footnote in document.Footnotes)
    {
        // Access footnote content.
        SubDocument footnoteContent = footnote.BeginUpdate();
        //...
        // Update all fields in the footnote.
        footnoteContent.Fields.Update();
        // Finalize to edit the footnote.
        footnote.EndUpdate(footnoteContent);
    }

    // Check all document endnotes.
    foreach (Note endnote in document.Endnotes)
    {
        // Access endnote content.
        SubDocument endnoteContent = endnote.BeginUpdate();
        //...
        // Update all fields in the endnote.
        endnoteContent.Fields.Update();
        // Finalize to edit the endnote.
        endnote.EndUpdate(endnoteContent);
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
    ' Access a document.
    Dim document As Document = wordProcessor.Document
  ' Check all document footnotes.
    For Each footnote As Note In document.Footnotes
        ' Access footnote content.
        Dim footnoteContent As SubDocument = footnote.BeginUpdate()
        '...
        ' Update all fields in the footnote.
        footnoteContent.Fields.Update()
        ' Finalize to edit the footnote.
        footnote.EndUpdate(footnoteContent)
    Next footnote

    ' Check all document endnotes.
    For Each endnote As Note In document.Endnotes
        ' Access endnote content.
        Dim endnoteContent As SubDocument = endnote.BeginUpdate()
        '...
        ' Update all fields in the endnote.
        endnoteContent.Fields.Update()
        ' Finalize to edit the endnote.
        endnote.EndUpdate(endnoteContent)
    Next endnote
End Using

The following code snippets (auto-collected from DevExpress Examples) contain references to the Update() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Field.cs#L22

csharp
document.Fields.Create(caretPosition, "DATE");
document.Fields.Update();

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/DocumentFieldActions.cs#L17

csharp
document.Fields.Create(document.CaretPosition, "DATE");
document.Fields.Update();
document.EndUpdate();

word-document-api-examples/CS/CodeExamples/FieldActions.cs#L31

csharp
// Update all fields in the main document body.
document.Fields.Update();

how-to-use-docvariable-fields/CS/DocumentVariablesExample/Form1.cs#L106

csharp
richEditControl2.Options.Fields.UpdateLockedFields = UpdateLockedFields.DocVariableOnly;
richEditControl2.Document.Fields.Update();
#endregion #updatedocvariablefields

word-document-api-insert-dynamic-content/CS/Program.cs#L22

csharp
wordProcessor.CalculateDocumentVariable += WordProcessor_CalculateDocumentVariable;
wordProcessor.Document.Fields.Update();
wordProcessor.SaveDocument("MergedDocument.docx", DocumentFormat.OpenXml);

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Field.vb#L19

vb
document.Fields.Create(caretPosition, "DATE")
document.Fields.Update()
'Finalize the modification

wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/DocumentFieldActions.vb#L14

vb
document.Fields.Create(document.CaretPosition, "DATE")
document.Fields.Update()
document.EndUpdate()

word-document-api-examples/VB/CodeExamples/FieldActions.vb#L30

vb
' Update all fields in the main document body.
document.Fields.Update()
' Finalize to edit the document.

how-to-use-docvariable-fields/VB/DocumentVariablesExample/Form1.vb#L102

vb
richEditControl2.Options.Fields.UpdateLockedFields = UpdateLockedFields.DocVariableOnly
            richEditControl2.Document.Fields.Update()
#End Region ' #updatedocvariablefields

word-document-api-insert-dynamic-content/VB/Module1.vb#L17

vb
AddHandler wordProcessor.CalculateDocumentVariable, AddressOf WordProcessor_CalculateDocumentVariable
wordProcessor.Document.Fields.Update()
wordProcessor.SaveDocument("MergedDocument.docx", DocumentFormat.OpenXml)

See Also

FieldCollection Interface

FieldCollection Members

DevExpress.XtraRichEdit.API.Native Namespace