Replacing Custom Properties

Hello

To add a custom property in VBA:

Dim swApp As Object

Sub main()

Set swApp = Application.SldWorks

Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim bret As Boolean

Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

bret = cusPropMgr.Add3("Designation4", swCustomInfoType_e.swCustomInfoText, "test Designation 4", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)

End Sub

Kind regards

So to add a property, read the value of a property and delete a property we can do:

Dim swApp As Object

Sub main()

Set swApp = Application.SldWorks

Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim bret As Boolean
Dim lRetVal As Long
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim vPropTypes As Variant
Dim vPropValues As Variant
Dim resolved As Variant
Dim custPropType As Long
Dim K As Long
Dim NomProperty As String

Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

' On ajoute une propriété
bret = cusPropMgr.Add3("Designation5", swCustomInfoType_e.swCustomInfoText, "test Designation5", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
    cusPropMgr.Get2 vPropNames(K), ValOut, ResolvedValOut
    custPropType = cusPropMgr.GetType2(vPropNames(K))
    ' On relève la valeur de la propriété "Designation5"
    If vPropNames(K) = "Designation5" Then
        NomProperty = ResolvedValOut
        MsgBox NomProperty
    End If
    ' On supprime la propriété "Designation5"
    If vPropNames(K) = "Designation5" Then
        cusPropMgr.Delete (vPropNames(K))
    End If
Next K

End Sub

Kind regards

Thank you D. Roger for your reward, it works except for the values of properties that contain quotation marks for example " $PRP:"SW-File Name", in the macro defaults   "compilation  error Expected list separator or )", I understand the problem well what is written in the properties "mixes" with the VB code how to tell it that the quotation marks do not not part of the code but of the value to be written in the box?

you write as follows (with all the quotation marks): "$PRP:""SW-File Name"""

Kind regards

1 Like

Super perfect, everything works as it should! I'll look later to be able to run the amcro in burst on several files.