VBA Solidworks, retrieving a custom property of a part

Hi all

I code a lot on VBA excel, and I would like to use it in SW.

I would like to extract the custom data of a part on an excel doc.

So if someone can help me just make me appear a personalized property of a room in msgbox after I get ready for the rest. 

 

Thanks in advance

 

 

Hello

See code below:

Option Explicit

Sub main()

Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swModelDocExt           As SldWorks.ModelDocExtension

Dim val                     As String
Dim valout                  As String
Dim bool                    As Boolean

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim K As Long

Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
    bool = cusPropMgr.Get4(vPropNames(K), False, val, valout)
    MsgBox vPropNames(K) & " - " & val & " - " & valout
Next K

End Sub

Kind regards

2 Likes

Hello

Subject already discussed several times but: http://help.solidworks.com/2016/english/api/sldworksapi/Get_Custom_Properties_of_Referenced_Part_Example_VB.htm

The examples above allow you to retrieve the custom properties of the "Customize" tab, if you also want to retrieve those of the "Configuration specific" tab for each configuration then you can see an example on the following link: http://help.solidworks.com/2013/English/api/sldworksapi/Get_Custom_Properties_Example_VB.htm

Kind regards

Hello everyone and thank you for your responsiveness. Mine is not so good because I'm very busy at the moment...

I had noticed the different posts, but when in my room I have the custom property "Finishing" and I want a msgbox "RAL9010" when I run my macro, I can't do it...  

 

 

Hello

If you could post your macro it would be easier.

Hello

If you want to control Solidworks from Excel you have tothink about declaring SolidWorks in Excel with a "CreateObject" and putting the necessary references in your macro.  The macro in the first post works and displays an msgbox including the "Property Name - Text Expression - Evaluated Value" for each property in the customize tab.

Kind regards

Therefore:

and:

Sub Macro1()
    
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swModelDocExt           As SldWorks.ModelDocExtension

Dim val                     As String
Dim valout                  As String
Dim bool                    As Boolean

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
        
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim K As Long

Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
    bool = cusPropMgr.Get4(vPropNames(K), False, val, valout)
    MsgBox vPropNames(K) & " - " & val & " - " & valout
Next K

End Sub

 

Gives:

Kind regards

In fact, it works very well, it's just that I confused the custom property and the configuration-specific lzq... what a noodle pffff

Is it possible to do the same thing with the specifics?

Thank you all!

Hello

Yes, it's marked above:

The examples above allow you to retrieve the custom properties of the "Customize" tab, if you also want to retrieve those of the "Configuration specific" tab for each configuration then you can see an example on the following link: http://help.solidworks.com/2013/English/api/sldworksapi/Get_Custom_Properties_Example_VB.htm

Kind regards

1 Like

At the top!!

One last question: I am looking to recover the assessed value, and not the value. 

nNumProp = swConfig.GetCustomProperties(vPropName, vPropValue, vPropType)

Thanks in advance

Hello

For my part, I use this instead:

Set swCustProp = swConfig.CustomPropertyManager
boolstatus = swCustProp.Get5("xxx", False, ValOut, ResolvedValOut, WasResolved) 'Changer "xxx" par le nom de la propriété du fichier 3D

Valout = Value of the expression

ResolvedValout = Calculated Value

I agree with Cyril.f, here is an example to read the values "property name - Text expression - Value evaluated" for each property of the active config:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim lRetVal As Long
Dim vPropNames As Variant
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim nNbrProps As Long
Dim j As Long

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config.CustomPropertyManager

    nNbrProps = cusPropMgr.Count
    vPropNames = cusPropMgr.GetNames

    For j = 0 To nNbrProps - 1
        lRetVal = cusPropMgr.Get5(vPropNames(j), False, ValOut, ResolvedValOut, wasResolved)
        Debug.Print vPropNames(j) & ", " & ValOut & ", " & ResolvedValOut
    Next j

End Sub

Kind regards

1 Like

Or, if you know the name of your config, you just have to change the line:

Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

by:

Set cusPropMgr = swModelDocExt.CustomPropertyManager("xxxxxxx")

In the macro given in best answer, xxxxxxx being the name of the config.

Kind regards

1 Like

Perfect!!! I've finished my program and it's working perfectly...

Now I have to look to retrieve the custom properties of each part of an assembly...

Thanks again to the whole team.

1 Like

Hi everyone

I had a code that I had made under SW2014 that also worked wonderfully but that doesn't work anymore in SW2016.

Here is the code at issue:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As SldWorks.PackAndGo
Dim openFile As String
Dim myFileName As String
Dim pgFileNames As Variant
Dim pgFileStatus As Variant
Dim pgSetFileNames() As String
Dim pgGetFileNames As Variant
Dim pgDocumentStatus As Variant
Dim status As Boolean
Dim warnings As Long
Dim errors As Long
Dim i As Long
Dim j As Long
Dim namesCount As Long
Dim myPath As String
Dim statuses As Variant

Public Chemin, OldFile As String
Public Ligne1, DernligneASM

Sub ListerOldFichiers()

Dim Fichier As String

Range("A2:B1000") = "" 'Vidage des cellules

Chemin = CheminUser
OldFile = Dir(Chemin & "*.sldasm")

'Appel de la progressbar

UserForm1.Show vbModeless
UserForm1.ProgressBar1.Value = 0

Dim ProgressBar, barre

UserForm1.ProgressBar1.Value = 10

'Ecrire les noms de fichiers dans colone A

Ligne1 = 2 'Départ pour rentrer les noms de fichiers

    Do While OldFile <> ""
    
        Cells(Ligne1, 1) = OldFile
        OldFile = Dir()
        Ligne1 = Ligne1 + 1
    
    Loop

DernligneASM = Range("a65536").End(xlUp).Row

Dim Dernligne2
Dernligne2 = Range("a65536").End(xlUp).Row + 1

OldFile = Dir(Chemin & "*.sldprt")

    Do While OldFile <> ""
    
        Cells(Dernligne2, 1) = OldFile
        OldFile = Dir()
        Dernligne2 = Dernligne2 + 1
        
    Loop

UserForm1.ProgressBar1.Value = 50


Dim Dernligne3
Dernligne3 = Range("a65536").End(xlUp).Row

Ligne1 = 2

For Ligne1 = Ligne1 To Dernligne3

        Dim DSO As DSOFile.OleDocumentProperties
        Dim File1, OldDes, k, PropName, Compteur

        File1 = Cells(Ligne1, 1).Value

        Set DSO = New DSOFile.OleDocumentProperties
        DSO.Open sfilename:=Chemin & File1

        Compteur = DSO.CustomProperties.Count

        If Compteur <> 0 Then

            For k = 1 To Compteur - 1

                PropName = DSO.CustomProperties.Item(k).Name

                If PropName = "Designation-1" Then

                    OldDes = DSO.CustomProperties.Item("Designation-1").Value
                    Cells(Ligne1, 2) = OldDes

                End If

            Next k

        End If

        DSO.Save
        DSO.Close

Next

'Fini de remplir et Decharger l'userform

barre = 100
UserForm1.ProgressBar1.Value = barre
Unload UserForm1
ProgressBar = 0 'Réinitialisation

MsgBox "Remplissez la colonne des Nouveaux noms a attribuer puis cliquez sur ''Renommer''"

End Sub

 

The "DSO. CustomProperties.Count" no longer works... from what I could see, it would be the DSO part that is no longer taken into account. But I don't know what to replace it with...

Thank you for your help

 

 

Hello

It would be better to create a new question rather than queue a question that has already been solved, it is easier to find answers to given problems.

For your DSO problem that doesn't work anymore I think you'll have to re-write your macro, see https://forum.solidworks.com/thread/88676.

Kind regards

2 Likes

Oh sorry... Can we transfer this exchange to a new topic?

Thank you for your answer. What has been the method since 2015?

open another question thank you

@+

1 Like

There you have it: http://www.lynkoa.com/forum/solidworks/r%C3%A9cupererecrire-propri%C3%A9t%C3%A9s-personalis%C3%A9s-avec-excel-sans-methode-dso-inactive-depuis