vb.netms-wordgembox-documentdocvariable

Updating DOCPROPERTY fields with GemBox


My document has DOCPROPERTY fields and I am trying to update them (Title, SubTitle, etc.).

Dim title As String = "New Title"
Dim subTitle As String = "New Sub Title"
Dim document = DocumentModel.Load(inputFile)

document.DocumentProperties.BuiltIn(BuiltInDocumentProperty.Title) = title
document.DocumentProperties.Custom("SubTitle") = subTitle

document.Save(outputFile)

But the final document does not show the updated values, I can see them only after refreshing them in MS Word by pressing F9. How can I refresh them with GemBox.Document?

Also I have some DOCVARIABLE fields which are updated with macros. Can I update them with GemBox.Document?


Solution

  • UPDATE 02-01-2020:
    The current latest bugfix version for GemBox.Document introduced API support for Field.Update, so from now own the updating of both DOCPROPERTY and DOCVARIABLE fields can be simplified like the following:

    For Each field As Field In document.GetChildElements(True, ElementType.Field)
        field.Update()
    Next
    

    UPDATE 11-10-2017:
    The current latest bugfix version for GemBox.Document introduced API support for DocumentModel.Variables, so from now own the DOCVARIABLE fields can be updated, for example with something like the following:

    Dim variables As VariablesDictionary = document.Variables
    
    For Each field As Field In document.GetChildElements(True, ElementType.Field).Cast(Of Field)().Where(Function(f) f.FieldType = FieldType.DocVariable)
        Dim instruction As String = field.GetInstructionText()
        Dim variableName As String = If(instruction.IndexOf(" "c) < 0, instruction, instruction.Remove(instruction.IndexOf(" "c)))
        Dim value As String = Nothing
    
        If variables.TryGetValue(variableName, value) Then
            field.ResultInlines.Clear()
            field.ResultInlines.Add(New Run(document, value) With {.CharacterFormat = field.CharacterFormat.Clone()})
        End If
    Next
    

    ORIGINAL:
    GemBox.Document does not automatically update DOCPROPERTY fields when saving to DOCX file. However, they are updated when saving to PDF, XPS or image format and when printing.
    Nevertheless, you can update them with something like the following:

    Dim properties As DocumentProperties = document.DocumentProperties
    
    For Each field As Field In document.GetChildElements(True, ElementType.Field).Cast(Of Field)().Where(Function(f) f.FieldType = FieldType.DocProperty)
        Dim instruction As String = field.GetInstructionText()
        Dim propertyName As String = If(instruction.IndexOf(" "c) < 0, instruction, instruction.Remove(instruction.IndexOf(" "c)))
        Dim value As String = Nothing
    
        Dim customValue As Object = Nothing
        Dim buildInProperty As BuiltInDocumentProperty
    
        If properties.Custom.TryGetValue(propertyName, customValue) Then
            value = customValue.ToString()
        ElseIf [Enum].TryParse(propertyName, buildInProperty) Then
            properties.BuiltIn.TryGetValue(buildInProperty, value)
        End If
    
        If Not String.IsNullOrEmpty(value) Then
            field.ResultInlines.Clear()
            field.ResultInlines.Add(New Run(document, value) With {.CharacterFormat = field.CharacterFormat.Clone()})
        End If
    Next
    

    Also regarding the DOCVARIABLE fields, these ones cannot currently be updated with GemBox.Document.