This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This VBA macro sorts the custom properties in a file and all configurations using the logical order with SOLIDWORKS API. Both ascending and descending order can be specified.
Logical order sorts the element as follows. This is an order of files being ordered in Windows File Explorer
Property1
Property2
Property3
Property12
Property20
Property21
Property30
While alphabetical sort for the above sequence would produce the following result:
Property1
Property12
Property2
Property20
Property21
Property3
Property30
Configuration
Macro can be configured by changing the constant values in the macro as follows:
ConstASCENDINGAsBoolean = True'True to sort ascending, False to sort descendingConst REORDER_GENERAL_CUST_PRPS AsBoolean = True'True to sort file specific custom properties, False to skipConst REORDER_CONF_CUST_PRPS AsBoolean = True'True to sort configuration specific custom properties (for parts and assemblies), False to skip
Declare PtrSafe Function StrCmpLogicalW Lib"shlwapi" (ByVal s1 AsString, ByVal s2 AsString) AsIntegerConstASCENDINGAsBoolean = TrueConst REORDER_GENERAL_CUST_PRPS AsBoolean = TrueConst REORDER_CONF_CUST_PRPS AsBoolean = TrueDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenIf REORDER_GENERAL_CUST_PRPS ThenDim swCustPrpMgr As SldWorks.CustomPropertyManager
Set swCustPrpMgr = swModel.Extension.CustomPropertyManager("")
ReorderProperties swCustPrpMgr, ASCENDINGEndIfIf REORDER_CONF_CUST_PRPS ThenDim vConfNames AsVariant
vConfNames = swModel.GetConfigurationNames
IfNot IsEmpty(vConfNames) ThenDim i AsIntegerFor i = 0 To UBound(vConfNames)
Dim swConfCustPrpMgr As SldWorks.CustomPropertyManager
Set swConfCustPrpMgr = swModel.Extension.CustomPropertyManager(vConfNames(i))
ReorderProperties swConfCustPrpMgr, ASCENDINGNextEndIfEndIf
swModel.SetSaveFlag
Else
MsgBox "Please open document"EndIfEndSubSub ReorderProperties(custPrpMgr As SldWorks.CustomPropertyManager, asc AsBoolean)
Dim vPrpNames AsVariantDim vPrpTypes AsVariant'NOTE: returned properties values are resolved for both valOut and resValOut parameters
custPrpMgr.GetAll2 vPrpNames, vPrpTypes, Empty, Empty
IfNot IsEmpty(vPrpNames) ThenDim dict AsObjectSet dict = CreateObject("Scripting.Dictionary")
Dim i AsIntegerFor i = 0 To UBound(vPrpNames)
Dim prpVal AsString
custPrpMgr.Get3 vPrpNames(i), False, prpVal, ""
dict.Add vPrpNames(i), Array(vPrpTypes(i), prpVal)
custPrpMgr.Delete2 vPrpNames(i)
Next
vPrpNames = BubbleSort(vPrpNames, asc)
For i = 0 To UBound(vPrpNames)
Dim vPrpData AsVariant
vPrpData = dict.Item(vPrpNames(i))
If custPrpMgr.Add3(vPrpNames(i), vPrpData(0), vPrpData(1), swCustomPropertyAddOption_e.swCustomPropertyOnlyIfNew) <> swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged Then
Err.Raise vbError, "", "Failed to add property"EndIfNextEndIfEndSubFunction BubbleSort(vStrArray AsVariant, asc AsBoolean) AsVariantDim swapPos AsInteger
swapPos = IIf(asc, 1, -1)
Dim vResStrArray AsVariant
vResStrArray = vStrArray
Dim i AsIntegerDim j AsIntegerDim tempVal AsStringFor i = 0 To UBound(vResStrArray)
For j = i To UBound(vResStrArray)
If StrCmpLogicalW(StrConv(CStr(vResStrArray(i)), vbUnicode), StrConv(CStr(vResStrArray(j)), vbUnicode)) = swapPos Then
tempVal = vResStrArray(j)
vResStrArray(j) = vResStrArray(i)
vResStrArray(i) = tempVal
EndIfNextNext
BubbleSort = vResStrArray
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expert Artem Taturevych at 3DEXPERIENCE World 2026 on Wednesday, Feb 4 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more