VBA: General Personal Properties and Not Configuration Properties

Hi all

I don't master VBA very well and I fiddle a little...

So I created this macro (by copying it and adding my properties) but I would like the created properties to be put in the "Customize" tab and not in "Configuration specific". Could you correct my macro please? Thank you in advance.

Dim swApp As Object
Dim swModel As ModelDoc2
Dim config As SldWorks.Configuration
Dim swCustProp As CustomPropertyManager
Dim lRetVal As Long
Dim myValue0 As String
Dim myValue1 As String
Dim myValue2 As String
Dim myValue3 As String
Dim myValue4 As String

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    my0Value = swModel.GetTitle 'We get the file name
    myValue1 = Left(myValue0, 16) 'We get the part number, so the first 16 characters of the file name
    myValue2 = Left(myValue0, 22) 'We get the first 22 characters of the file name
    myValue3 = Right(myValue0, Len(myValue0) - Len(myValue2)) 'We get the Designation last characters of the file name - the first 22
    myValue4 = Left(myValue0, 13) 'We get the mold number
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config. CustomPropertyManager
    lRetVal = cusPropMgr.Add3("PartNumber", swCustomInfoType_e.swCustomInfoText, myValue1, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
    lRetVal = cusPropMgr.Add3("Designation", swCustomInfoType_e.swCustomInfoText, myValue3, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
    lRetVal = cusPropMgr.Add3("DocumentSource", swCustomInfoType_e.swCustomInfoText, myValue4, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub

 

If you have a good tutorial to recommend for the VBA Solidworks I'm all for it.

Hello;

To make your properties in the 'Custom' tab (and not in 'Configuration'):
Replace your line:
Set cusPropMgr = config. CustomPropertyManager
by
Set cusPropMgr =swModel.Extension.CustomPropertyManager("")

For a tutorial: see here (in English but well explained)
https://blog.codestack.net/custom-properties-automation
Or if you're really just starting out:
https://thecadcoder.com/Solidworks-macro-guide/
without forgetting the "F1" key in the VB editor
Neither the Solidworks API Help


Note: your code is not very long, but in the future it would be better to use the Code editor


(in VBscript mode for VB macros... for Visual Basic-)

Kind regards

3 Likes

Hello

and more generally, prefer vbNullString rather than "". Faster to run and lighter in memory.

Hello @guillaume.bruere 

The answer to your question that deserves to be marked as a solution is @Maclane's message , not mine! Thank you for correcting for him.

Kind regards

1 Like

Hello @Sylk, yes I wanted to approve the 2 answers but only one is taken into account. Thank you all.

1 Like

Hello;

Thanks Sylk but since I didn't know how to use "vbNullString", I'm willing to give you back 1/2 point... ;)
Kind regards.

2 Likes