how can I sort custom document properties by name
EDIT: it can be done through VBA (see below)
How can I sort the custom document properties in word by their name in ascending order?
There is no sort or reorder option in the document properties dialog.
I have even tried to unzip the .docx file, and edit the docProps\custom.xml file and reorder the <property-elements in the xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
`<property fmtid="{CDDD5892-2E9C-93AC-883D-CF8323ADAE08}" pid="6" name="foo">`
`<vt:lpwstr>bli</vt:lpwstr>`
`</property>`
`<property fmtid="{CDDD5892-2E9C-93AC-883D-CF8323ADAE08}" pid="2" name="bar">`
`<vt:lpwstr>bla</vt:lpwstr>`
`</property>`
`<property fmtid="{CDDD5892-2E9C-93AC-883D-CF8323ADAE08}" pid="12" name="lorem">`
`<vt:lpwstr>blu</vt:lpwstr>`
`</property>`
</Properties>
but this did not work as intended.
It is quite annoying when having multiple document properties but you can't order them in ascending order and need to "search" the list to adjust the property in question.
Any ideas?
I might probably delete all custom properties and then recreate them in the correct order - but that did not work.
okay, it works through VBA:
- add a VBA module to your .docx - as long as you don't hit "save" you can use VBA within a .docx
- select the code below and copy it into the new VBA module
- put your cursor into the VBA-Immediate-windows (or hit Ctrl+G)
- type 'ReorderCustomDocumentProperties' without the quotes and hit ENTER
- voilà
If anyone has a smoother, shorter way to re-order the custom document properties, please let me know!
Option Explicit
'
'module created 13.08.2026 / SiSRT
'
Public Sub ReorderCustomDocumentProperties()
'purpose: reorders all custom document property alphabetically by name
Dim i As Integer
Dim blnResult As Boolean
Dim varCustomDocumentProperties As Variant
Dim varCustomDocumentPropertiesSorted As Variant
'get all custom document propertes
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "retrieving all custom document properties ..."
varCustomDocumentProperties = GetAllCustomDocumentProperties()
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "properties found: " & UBound(varCustomDocumentProperties, 1)
'delete all custom document properties
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "deleting all custom document properties ..."
For i = 0 To UBound(varCustomDocumentProperties, 1)
blnResult = DeleteCustomDocumentProperty(CStr(varCustomDocumentProperties(i, 0)))
If Not blnResult Then
Err.Raise -2, , "failed to delete CDP '" & CStr(varCustomDocumentProperties(i, 0)) & "'"
End If
Next i
'sort all custom document property by name
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "sorting custom document properties ..."
varCustomDocumentPropertiesSorted = BubbleSortArray(varCustomDocumentProperties, 0)
'add all stored document properties again
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "adding custom document properties ..."
For i = 0 To UBound(varCustomDocumentPropertiesSorted, 1)
blnResult = AddCustomDocumentProperty(CStr(varCustomDocumentPropertiesSorted(i, 0)), CStr(varCustomDocumentPropertiesSorted(i, 1)), CInt(varCustomDocumentPropertiesSorted(i, 2)))
If Not blnResult Then
Err.Raise -3, , "failed to add CDP '" & CStr(varCustomDocumentPropertiesSorted(i, 0)) & "'"
End If
Next i
Debug.Print VBA.Now, "ReorderCustomDocumentProperties()", "done! check custom document properties now!"
End Sub
Public Function GetAllCustomDocumentProperties() As Variant
'purpose: get all custom document properties as 2D-Array
'returns: 2D-Array with (name, value, type)
Dim i As Integer
Dim prop As DocumentProperty
Dim varCustomDocumentProperties As Variant
ReDim varCustomDocumentProperties(ActiveDocument.CustomDocumentProperties.Count - 1, 2)
i = 0
For Each prop In ActiveDocument.CustomDocumentProperties
varCustomDocumentProperties(i, 0) = prop.Name
varCustomDocumentProperties(i, 1) = prop.value
varCustomDocumentProperties(i, 2) = prop.Type
i = i + 1
Next prop
GetAllCustomDocumentProperties = varCustomDocumentProperties
End Function
Public Function AddCustomDocumentProperty(strName As String, strValue As String, intMsoDocProperty As MsoDocProperties) As Boolean
'purpose: simple method to add a custom document property
Dim blnResult As Boolean
On Error GoTo ERROR_HANDLER
blnResult = True
ActiveDocument.CustomDocumentProperties.Add _
Name:=strName, _
LinkToContent:=False, _
Type:=intMsoDocProperty, _
value:=strValue
EXIT_FUNCTION:
AddCustomDocumentProperty = blnResult
Exit Function
ERROR_HANDLER:
Debug.Print VBA.Now, "AddCustomDocumentProperty()", "Error#" & Err.Number, Err.Description
blnResult = False
Resume EXIT_FUNCTION
End Function
Public Function DeleteCustomDocumentProperty(strName As String) As Boolean
'purpose: simple function to delete a document custom property
'returns: true, if custom document property was delete
'to do: differentiate error between property not found and can't delete property
Dim blnResult As Boolean
Dim prop As DocumentProperty
blnResult = False
On Error GoTo ERROR_HANDLER
Set prop = ActiveDocument.CustomDocumentProperties(strName)
If Not prop Is Nothing Then
Debug.Print VBA.Now, "DeleteCustomDocumentProperty()", "deleting " & prop.Name & "... ";
prop.Delete
blnResult = True
Debug.Print "deleted";
End If
Debug.Print ""
EXIT_FUNCTION:
DeleteCustomDocumentProperty = blnResult
Exit Function
ERROR_HANDLER:
Debug.Print VBA.Now, "DeleteCustomDocumentProperty()", "Error#" & Err.Number, Err.Description
blnResult = False
Resume EXIT_FUNCTION
End Function
Public Function BubbleSortArray(arr As Variant, Optional intSortColumtIndex As Integer = 0) As Variant
'purpose: q'n'd bubble sort for 1D and 2D-arrays
Dim i As Long
Dim j As Long
Dim n As Long
Dim varResultArray As Variant
Dim temp As Variant
varResultArray = arr
If DimensionExists(varResultArray, 2) Then
'sort a 2D-Array
For i = LBound(varResultArray, 1) To UBound(varResultArray, 1) - 1
For j = i + 1 To UBound(varResultArray, 1)
If varResultArray(i, intSortColumtIndex) > varResultArray(j, intSortColumtIndex) Then
For n = LBound(varResultArray, 2) To UBound(varResultArray, 2) - 1
temp = varResultArray(i, n)
varResultArray(i, n) = varResultArray(j, n)
varResultArray(j, n) = temp
Next n
End If
Next j
Next i
Else
'sort a 1D-Array
For i = LBound(varResultArray) To UBound(varResultArray) - 1
For j = i + 1 To UBound(varResultArray)
If varResultArray(i) > varResultArray(j) Then
temp = varResultArray(i)
varResultArray(i) = varResultArray(j)
varResultArray(j) = temp
End If
Next j
Next i
End If
BubbleSortArray = varResultArray
End Function
Public Function DimensionExists(arr As Variant, intDimension As Integer) As Boolean
'purpose: helper to decide wether it is a 1D or 2D-Array
Dim blnResult As Boolean
On Error GoTo ERROR_HANDLER
blnResult = (UBound(arr, intDimension) = UBound(arr, intDimension))
EXIT_FUNCTION:
DimensionExists = blnResult
Exit Function
ERROR_HANDLER:
blnResult = False
Resume EXIT_FUNCTION
End Function
Public Sub PrintArray(arr As Variant, Optional strColDelimiter As String = ",", Optional strRowDelimiter As String = vbCrLf)
'purpose: helper to print the elements of an array into the immediate window
Dim i As Integer
Dim j As Integer
If DimensionExists(arr, 2) Then
'print 2D-Array
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
If j <> UBound(arr, 2) Then
Debug.Print arr(i, j) & strColDelimiter;
Else
Debug.Print CStr(arr(i, j)) & strColDelimiter
End If
Next j
Next i
Else
'print 1D-Array
For i = LBound(arr) To UBound(arr)
If i <> UBound(arr) Then
Debug.Print arr(i) & strColDelimiter;
Else
Debug.Print CStr(arr(i));
End If
Next i
End If
End Sub
Posted Aug 13, 2026, 4:14 PM UTC