1 Reply Latest reply on Jan 11, 2014 1:04 AM by Simon Turner

    Populate array for SolidWorks Components and then use it

    Steve Anderson

      The program errors here: swComp = swCompArr(i) with Rin-time error 91, Object Variable or With block variable not set

       

       

      Sub main()

       

       

          ' Connect to SolidWorks

          Dim swApp As SldWorks.SldWorks

          Set swApp = Application.SldWorks

        

          ' Assembly, Current active document if it is an assembly

          Dim swAssy As SldWorks.AssemblyDoc

          Set swAssy = swApp.ActiveDoc

                 

          Dim swComp As SldWorks.Component2

             

          Dim i As Integer

          i = 0

         

          Dim c As Integer

          c = swAssy.GetComponentCount(True)

         

          MsgBox c

         

          Dim swCompArr As Variant

          swCompArr = swAssy.GetComponents(True)

         

          Do While Not i = c

              If Not IsEmpty(swCompArr) Then

                  swComp = swCompArr(i)

                 

                  ' Component Part No must begin with 242 and not be supressed

                  If swComp.IsSuppressed = 1 Or Left(swComp.Name, 3) <> "242" Then

                      ' Do Nothing

                                                                              

                  Else

                      ' Set Component

                      Dim swModel2 As SldWorks.ModelDoc2

                      swModel2 = swComp.GetModelDoc2

       

       

                      Dim swModelDocExt As SldWorks.ModelDocExtension

                      swModelDocExt = swModel2.Extension

                         

                      Dim swSelMgr As SldWorks.SelectionMgr

                      swSelMgr = swModel2.SelectionManager

                         

                      Dim bComp As Boolean

                      bComp = swModelDocExt.SelectByID2("#20 (0.161) Diameter Hole1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

                     

                      Dim Feature As SldWorks.Feature

                      Feature = swComp.FirstFeature

                     

                      Do While Not Feature Is Nothing

                          If Feature = "#20 (0.161) Diameter Hole1" Then

                              MsgBox "Found " & Feature.Name

                             

                          End If

                         

                          Feature = Feature.GetNextFeature

                         

                      Loop

                             

       

       

                      ' Project points

       

       

                  End If

             

              End If

              i = i + 1

             

       

       

          Loop

         

      End Sub