AnsweredAssumed Answered

Select component by name in assembly

Question asked by Pichaiyan Prabaharan on Jan 26, 2018
Latest reply on Jan 26, 2018 by Ulf Stockburger

I want to select component by name  for example Sub-assembly / part name contains  " xxx" .

I managed to filter the components by its name (refer the code below). but I not able to select those components.

Anyone kindly help with this.

Thanks a lot.



Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim vComps As Variant
Dim swComp As SldWorks.Component2
Dim i As Integer
Dim j As Integer
Dim FileName As String
Dim SearchString As String

' Enter the string here to find
Const FindChar As String = "PD"

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    swModel.ClearSelection2 True
    vComps = swAssy.GetComponents(False)
    For i = 0 To UBound(vComps)
    Set swComp = vComps(i)
    FileName = swComp.Name2
    SearchString = FileName
        For j = 1 To Len(SearchString)
            If Mid(SearchString, j, 2) = FindChar Then
              swComp.Select2 True, -1
                Debug.Print FileName
            End If
        Next j
    Next i
End Sub