AnsweredAssumed Answered

Opening Parts in an Assembly using VBA?

Question asked by Michael Chamerski on Jun 14, 2016
Latest reply on Jun 15, 2016 by Chiheb Gouta

Hey everyone!

 

I have a question as far as opening components within an assembly using VBA. Is it possible, if at all, to create a macro that will open parts, in order, from the tree of an assembly, and perform a function on that part, and finally saving and closing that part then moving on to the next part?

 

I was thinking I could start a For Loop, and taking the number of runs to be the total amount of components, like this:

 

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssembly As SldWorks.AssemblyDoc
Dim CompNum As String

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssembly = swApp.ActiveDoc

    ' Get number of components, not including child components in subassemblies
    CompNum = swAssembly.GetComponentCount(True)

     BEGIN LOOP USING CompNum amount of loops
          *****open part from assembly function (if subassembly, create another for-loop with same procedure)
          *****perform function on part
          *****save and close part, back to main assembly
     END LOOP

End main

 

Is there any simple way of doing this? Essentially, I would like to automate something that needs to be applied to each part / component within an assembly or subassembly.

 

Thanks in advance

 

Regards,

Michael

Outcomes