Macro vba to export custom properties of an assembly and its children

Hello

I would like to create a macro to export some custom properties of an assembly already open in solidworks but also of all these children.

I've already made a macro that pulls out the custom properties of an assembly or a part and saves this in an excel. (everything works well)

My problem is very simple. I can't open the parts since the assembly.
I can see that it's an assembly, I can go through the tree and find the complete path of all the children but I can't open each child to extract the custom properties...
If you have an idea I'm all for it.
Below is my code without the custom properties recovery part because that's not the problem.
The line " swApp.OpenDoc6 swChildComp.GetPathName(), 1, 0, "", 0, 0 " should open the children if I am mistaken.
The debug print " Debug.Print indent & swChildComp.Name2 & " (" & swChildComp.GetPathName() & " ) " gives me the way to the children in question.

Texte préformaté’**********************
'Copyright(C) 2023 Xarial Pty Limited
'Reference: Traversing the components tree using SOLIDWORKS API
'License: License
'**********************
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim Part As SldWorks.ModelDoc2
Dim SelMgr As SldWorks.SelectionMgr
Dim swFeatMgr As SldWorks.FeatureManager
Dim path_complete As String 'chemin complet de la pièce
Dim myError As Long
Dim myWarning As Long

Dim guillemet As String
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Dim ext As String
Const INDENT_SYMBOL As String = " "

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then


Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Set swFeatMgr = Part.FeatureManager




    Dim swRootComp As SldWorks.Component2

    Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent
    
 ' MsgBox "avant de rentrer dans la première boucle des enfants"
  
    TraverseComponent swRootComp, ""
    

Else

    MsgBox "Please open assembly"

End If

End Sub

Sub TraverseComponent(comp As SldWorks.Component2, indent As String)

Dim vChildComps As Variant

vChildComps = comp.GetChildren

Dim i As Integer

For i = 0 To UBound(vChildComps)

    Dim swChildComp As SldWorks.Component2
    Set swChildComp = vChildComps(i)
        
    Debug.Print indent & swChildComp.Name2 & " (" & swChildComp.GetPathName() & ")"

’ Ouvre le fichier de la boucle

ext = Right(swChildComp.GetPathName(), 6)

'si c’est un part
If ext = « sldprt » Then
'Debug.Print "Part " & swChildComp.GetPathName()

'swApp.OpenDoc6 "C:\VueLocalePDM\PDM\2-CREE\01-Bibliotheque\Pièces\Pièces 06\DOC-000024428.sldprt", 1, 0, "", 0, 0

swApp.OpenDoc6 swChildComp.GetPathName(), 1, 0, «  », 0, 0

Else
'ce n’est pas un prt"
If ext = « sldasm » Then

'////aSM
Set Part = swApp.OpenDoc6(swChildComp.GetPathName(), 2, 0, «  », longstatus, longwarnings) '2 pou swDocASSEMBLY

Else
MsgBox "pas d'extension trouvée"
End If

End If

MsgBox "avant fermeture du fichier"

''ferme le fichier de la boucle

'swApp.CloseDoc Part.GetPathName

Set Part = Nothing

Next

End Sub

For me your part is already open (in the assembly) so no opendoc6 but rather activatedoc3 (to make the already open part active):

swApp.ActivateDoc3(sOutputFolder & ".sldprt", False, swRebuildOnActivation_e.swUserDecision, Errors)

https://help.solidworks.com/2023/English/api/sldworksapi/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.ISldWorks~ActivateDoc3.html

1 Like

:rofl::rofl: :rofl:
… Where the limits of machine translation!

Fin du Sous Marin < = > End Sub
1 Like

This machine translation (Bing) for the code is indeed a disaster!

Impossible to post a long enough code without translation problems if you don't use the appropriate tag (preformatted text tag):
image

@flaheurte be careful to post the code via this tag otherwise end if becomes end if and multitude of other errors more or less amusing, certainly but which does not help to understand the code!

1 Like

Ok for me the opendoc allowed you to have it in a separate window.
I confirm that activatedoc3 does the job
Sorry for the preformatted text.

and Thank you!

1 Like

=> Don't be sorry when it's funny! :grin:

1 Like