How to use OpenXML SDK in a vb.net project to change custom properties of .docx files?

Aaron Gerber 20 Reputation points
2024-12-06T19:28:25.2+00:00

I'm working on a vb.net project that is supposed to use OpenXML SDK to alter custom file properties of a .docx file. Once I have the document open as a 'WordprocessingDocument`, how do I access the custom file properties?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,761 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,961 Reputation points Microsoft Vendor
    2024-12-09T07:44:12.51+00:00

    Hi @Aaron Gerber ,

    The custom file properties are stored in the CustomFilePropertiesPart of the document. You can access this part through the MainDocumentPart.

    If the CustomFilePropertiesPart does not exist, you will need to create it before you can add or modify properties.

    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.CustomProperties
    Imports DocumentFormat.OpenXml.VariantTypes
    
        Sub ModifyCustomProperties(docPath As String)
            Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(docPath, True)
                Dim customPropsPart As CustomFilePropertiesPart = wordDoc.CustomFilePropertiesPart
    
                If customPropsPart Is Nothing Then
                    customPropsPart = wordDoc.AddCustomFilePropertiesPart()
                    ' Add initial properties if needed
                    customPropsPart.Properties = New Properties()
                End If
    
                Dim properties = customPropsPart.Properties
    
                ' Check if a property exists by name
                Dim existingProp = properties.OfType(Of CustomDocumentProperty)().
                                   FirstOrDefault(Function(p) p.Name.Value = "YourPropertyName")
    
                If existingProp IsNot Nothing Then
                    existingProp.VTLPWSTR.Text = "NewValue"
                Else
                    Dim newPropId = If(properties.Elements(Of CustomDocumentProperty).Any(),
                                        properties.Elements(Of CustomDocumentProperty).Max(Function(p) CInt(p.PropertyId.Value)) + 1,
                                        1)
                    Dim newProp = New CustomDocumentProperty() With {
                        .Name = "YourPropertyName",
                        .PropertyId = newPropId,
                        .VTLPWSTR = New VTLPWSTR("YourValue")
                    }
                    properties.AppendChild(newProp)
                End If
    
                customPropsPart.Properties.Save()
            End Using
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.