AnsweredAssumed Answered

Having issue retrieving component name using Name2

Question asked by Ninad Kulkarni on Dec 26, 2018
Latest reply on Dec 27, 2018 by Ninad Kulkarni

I am trying to make a macro for adding components & applying mates. For adding mates I need to access the name of the component that I have added. Below is my test code for the said part. Once I get the name I can set up the mates using another user-defined function ( I have this already).

 

I am getting a runtime error '91': Object Variable or With Block variable not set.

I can't figure out I am going wrong being a green coder. Can anyone help?

(The file paths & component names are changed)

 

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

Dim swComp As SldWorks.Component2

 

Sub Main()

 

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    AssemblyTitle = swModel.GetTitle    ' Get title of assembly document

   

    Dim part_List(7) As String

      

    part_List(1) = "0001"

    part_List(2) = "0002"

    part_List(3) = "0003"

    part_List(4) = "0004"

    part_List(5) = "0005"

    part_List(6) = "0006"

    part_List(7) = "0007"

   

    Dim i As Integer

    For i = LBound(part_List) To UBound(part_List)

    sCompName = "File_path" + part_List(i) + ".SLDPRT"

    Set Part = swApp.OpenDoc6(sCompName, swDocPART, 0, "", errors, warnings)     'Open that specified part

    Set swModel = swApp.ActivateDoc3(AssemblyTitle, True, swUserDecision, errors)     ' Activate the assembly so that you can add the component to it

    Set swAssy = swModel

    Set swComp = swAssy.AddComponent5(sCompName, swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)

    Debug.Print (swComp.Name2())      

    swModel.ClearSelection2 (True)

    swModel.ViewZoomtofit2

    Next i

End Sub

 

Here is the sample code I am using

2019 SOLIDWORKS API Help - Add and Mate Component Example (VBA)

Outcomes