14 Replies Latest reply on Mar 17, 2015 12:53 AM by Deepak Gupta

    Macro Insert Component into open assembly

    Tiana Rosser

      I don't have any experience with programming languages so I am struggling to write a program which will insert a specific part into the current open assembly. I am using solid works 2013.


      Could someone please write the code required to do this :-)

        • Re: Macro Insert Component into open assembly
          Deepak Gupta

          Try these codes:


          Option Explicit

          Dim swApp As New SldWorks.SldWorks

          Dim swModel As ModelDoc2

          Dim Part As SldWorks.ModelDoc2

          Dim swAssy As AssemblyDoc

          Dim AssemblyTitle As String

          Dim sCompName As String

          Dim errors As Long

          Dim warnings As Long


          Sub Main()


              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc


              ' Get title of assembly document

              AssemblyTitle = swModel.GetTitle


              'Part details to be added

              sCompName = "Add Part path and namer here with extension"


              'Open that specified part

              Set Part = swApp.OpenDoc6(sCompName, swDocPART, 0, "", errors, warnings)


              ' Activate the assembly so that you can add the component to it

              Set swModel = swApp.ActivateDoc3(AssemblyTitle, True, swUserDecision, errors)

              Set swAssy = swModel


              ' Add the camtest part to the assembly document

              swAssy.AddComponent5 sCompName, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0


              swModel.ClearSelection2 (True)



          End Sub