23 Replies Latest reply on Nov 17, 2017 11:36 AM by Orb D.

    Solidworks 2017 Selection Macro Error

    Orb D.

      We have a macro that in previous versions of Solidworks, was able to cycle through the planes of a part. This macro helps us to mate quicker as we can quickly grab the planes on two parts and mate using just hotkeys. After upgrading to 2017, we have noticed this macro is no longer stable. It seems it will work for the first few minutes of using it and then randomly will error out on "Set swFeat = swSelComp.FirstFeature". It also seems this issue is only when selecting parts inside of assemblies. Again, this macro worked fine before 2017. Maybe these calls have changed in 2017?

       

      SCREENSHOT (002).png

       

      Thanks

        • Re: Solidworks 2017 Selection Macro Error
          Josh Brady

          I don't know of any changes...  It may be possible that there were changes regarding lightweight components?

          What error are you getting? Object Variable or With block not set?

          • Re: Solidworks 2017 Selection Macro Error
            Solid Air

            The call still looks valid.  As Josh asked, what error message are you getting.  Did you check your references?  Very difficult to debug without the code you are using.

            • Re: Solidworks 2017 Selection Macro Error
              Manikandan Babu

              Hi Orb,

               

              Can you provide the sample macro to verify.

               

              Thanks

              Manikandan

              • Re: Solidworks 2017 Selection Macro Error
                Josh Brady

                I'm running 2017 SP 4.1.  I'm unable to get it to crash however many levels down the component is in a subassembly.  The only way I can get a crash is to select a component from the tree that is suppressed, and then the error happens on a different line. 

                 

                When you get the error, does the macro continue to error out for the rest of the session, or does it work on subsequent runs?

                • Re: Solidworks 2017 Selection Macro Error
                  Orb D.

                  Attached is the sample macro. The error is random and doesnt happen every assembly, any specific amount of time, and doesnt seem to correlate with anything. Youll just be working with the macro fine for for a while and suddenly it doesnt work.

                   

                  SCREENSHOT.png


                  '-------------------------------------------------

                   

                   

                  ' Macro to select main reference geometry of an assembly component

                  ' for easy mating.  Modified extensively from the

                  ' "Select Origin of Assembly Component Example (VB)" in

                  ' SolidWorks and Add-Ins API Help

                   

                   

                  ' Preconditions:

                   

                   

                  '       (1) Assembly document is open.

                   

                   

                  '       (2) One or more items is selected.

                   

                   

                  '

                   

                   

                  ' Postconditions: One of the reference planes or the

                  ' origin of the last selected component is selected.

                   

                   

                  '

                   

                   

                  '--------------------------------------------------

                   

                   

                  Const STOPATORIGIN As Boolean = False

                  Const FIRSTREF As Long = 1

                  'Change the value of FIRSTREF above if you want

                  'one of the primary planes to be the first feature

                  'selected by the macro.  Values are:

                      'Front  = 1

                      'Top    = 2

                      'Right  = 3

                      'Origin = 4

                  '''''''''

                   

                   

                  Option Explicit

                   

                   

                  Sub main()

                   

                   

                      Dim swApp                       As SldWorks.SldWorks

                      Dim swModel                     As SldWorks.ModelDoc2

                      Dim swSelMgr                    As SldWorks.SelectionMgr

                      Dim swSelComp                   As SldWorks.Component2

                      Dim swCompModel                 As SldWorks.ModelDoc2

                      Dim swFeat                      As SldWorks.Feature

                      Dim bRet                        As Boolean

                      Dim GeneralSelObj               As Object

                      Dim myFeatureCollection         As New Collection

                      Dim i                           As Integer

                      Dim CurSelCount                 As Long

                      Dim MyTempPointObj              As Object

                      Dim mySelStr                    As String

                      Dim NewObjToSelect              As Object

                      Dim Chunks                      As Variant

                      Dim swVer                       As Variant

                      Dim ResolveIt                   As Integer

                      Dim DocTitle                    As String

                      Dim SuppressionState            As Integer

                      Dim suppressiontest             As SldWorks.Feature

                     

                      Dim mysubFeatureCollection   As New Collection

                      Dim j As Integer

                     

                   

                   

                   

                   

                   

                   

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                     

                      If swModel.GetType <> swDocASSEMBLY Then

                          MsgBox "This macro works on assembly documents only."

                          Exit Sub

                      End If

                     

                      Set swSelMgr = swModel.SelectionManager

                      CurSelCount = swSelMgr.GetSelectedObjectCount

                      If CurSelCount = 0 Then

                          MsgBox "Nothing was selected"

                          Exit Sub

                      End If

                     

                      Set GeneralSelObj = swSelMgr.GetSelectedObject(CurSelCount)

                      Set swSelComp = swSelMgr.GetSelectedObjectsComponent(CurSelCount)

                      swVer = Split(swApp.RevisionNumber, ".")

                      If CInt(swVer(0)) < 14 Then

                          If swSelComp.GetSuppression <> swComponentFullyResolved Then

                              If swSelComp.GetSuppression <> swComponentResolved Then

                                  ResolveIt = MsgBox("The component selected is not fully resolved." _

                                     & vbCrLf & "This functionality is only available for lightweight" & vbCrLf & _

                                     "components in SolidWorks 2006 or greater." & vbCrLf & vbCrLf & _

                                     "Resolve this component now?", vbYesNo, "Upgrade Time!")

                                  If vbYes = ResolveIt Then

                                      swSelComp.SetSuppression2 swComponentFullyResolved

                                  Else

                                      Exit Sub

                                  End If

                              End If

                          End If

                      End If

                   

                   

                      swSelMgr.DeSelect CurSelCount

                      Set swFeat = swSelComp.FirstFeature

                     

                     

                      Do While Not swFeat Is Nothing

                   

                   

                    

                          If "RefPlane" = swFeat.GetTypeName Then

                               mysubFeatureCollection.Add swFeat

                          End If

                         

                    

                          If "OriginProfileFeature" = swFeat.GetTypeName Then

                              Chunks = Split(swSelComp.Name2, "/")

                           

                             

                              If StrComp(Right(swModel.GetTitle, 7), ".sldasm", vbTextCompare) <> 0 Then

                                  DocTitle = swModel.GetTitle

                              Else

                                  DocTitle = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                              End If

                              'Debug.Print DocTitle

                             

                              mySelStr = "Point1@Origin@" & Chunks(0) & "@" & DocTitle

                              For i = 0 To (UBound(Chunks) - 1)

                                  mySelStr = mySelStr & "/" & Chunks(i + 1) & "@" & Left(Chunks(i), (InStrRev(Chunks(i), "-") - 1))

                             

                             

                              Next

                             

                   

                   

                   

                   

                              swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _

                                  0, 0, 0, True, 0, Nothing, swSelectOptionDefault

                              myFeatureCollection.Add swSelMgr.GetSelectedObject(swSelMgr.GetSelectedObjectCount)

                              swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _

                                  0, 0, 0, True, 0, Nothing, swSelectOptionDefault

                              If STOPATORIGIN Then

                                  Exit Do

                              End If

                          End If

                  'Debug.Print swFeat.GetNameForSelection(4)

                          Set swFeat = swFeat.GetNextFeature

                      Loop

                   

                   

                      Set NewObjToSelect = Nothing

                   

                   

                      For i = 1 To mysubFeatureCollection.Count

                     

                      Set suppressiontest = mysubFeatureCollection.Item(i)

                    

                          If suppressiontest.IsSuppressed = False Then

                     

                              myFeatureCollection.Add suppressiontest

                                  Debug.Print suppressiontest.GetNameForSelection(4)

                                  Debug.Print myFeatureCollection.Count

                     

                          End If

                   

                   

                      Next

                     

                      For i = 1 To myFeatureCollection.Count

                     

                     

                          If GeneralSelObj Is myFeatureCollection.Item(i) Then

                              Set NewObjToSelect = myFeatureCollection.Item((i Mod myFeatureCollection.Count) + 1)

                              'Use of Mod above cycles back to first item if last item matches.

                          End If

                     

                     

                      Next

                     

                     

                      If NewObjToSelect Is Nothing Then

                          Set NewObjToSelect = myFeatureCollection.Item(FIRSTREF)

                      End If

                   

                   

                      bRet = NewObjToSelect.Select(True): Debug.Assert bRet

                   

                   

                  End Sub

                   

                   

                  '-------------------------------------------------

                    • Re: Solidworks 2017 Selection Macro Error
                      Josh Brady

                      OK.  Let's try this question again...

                       

                      I understand that you can run the macro fine for some amount of time. I understand that it gives you an error after an undetermined amount of time.  I understand that you can't figure out what you're doing to make it error out. The question is, once you get the error, what do you have to do to make the macro work again?  Can you just run it again immediately and it works fine?  Or do you have to restart SW, reboot your pc, sacrifice a chicken/goat, etc. before it works again?

                    • Re: Solidworks 2017 Selection Macro Error
                      Solid Air

                      I need a little more information.  What does the user select before running the macro?  What is the end result when the macro is complete?

                       

                      What happens if the user selects too many?

                       

                      Edit 1:

                      Q:What happens if the user does not select a component?

                      Q: I do not see the macro cycling through the number of selected components, am I missing something?

                      Comment: So far I have found two obsolete calls but I do not think they are causing your issue.

                        • Re: Solidworks 2017 Selection Macro Error
                          Josh Brady

                          Just so you know, SA... I wrote this macro.  It replaces the last thing in the user's current selection set with one of the reference planes of the component owning that selection.  It doesn't matter how many selections there are, it just operates on the last thing selected. If nothing is selected when you run the macro, it tells you that nothing was selected.

                           

                          So, let's say, for example, you have 2 components whose top planes you want to mate.  Instead of hunting those planes in the feature tree, you just select any portion of the first component (edge, vertex, face, doesn't matter) and run the macro.  What you selected will be deselected, and the front plane is selected instead.  Immediately run again, and the right plane is selected instead.  One more time, and the top plane is selected.  Hold down Ctrl and select any portion of the next component.  Run the macro again.  The top plane of the first component remains selected, and the front plane of the next component is selected.  Run 2 more times to get to the top plane.  Add your mate.  This is extremely fast when you have it mapped to a shortcut key.

                           

                          I can't imagine what could be happening to make a certain assembly "toxic" to this macro on a given machine.  Especially if it runs just fine on the same assembly when opened on a different machine.  Edit: Just saw OP's edit about toxicity remaining on other machines. I use this macro all the time in my daily workflow, and have never seen this behavior.

                            • Re: Solidworks 2017 Selection Macro Error
                              Solid Air

                              Josh,

                              Thanks for the clarification.  I thought the user selected two components before running the macro.  And now I understand why the macro operates on the last selected component (so you can use the control key).  It also explains the results when I select different items.

                               

                              As I was writing this, I selected a plane in the top level assembly;  I got the run-time error 91 message.  Could this be Orb's issue?  Are you supposed to be able to select something in the top level assembly?  I will run the macro in previous SW versions to see if it errors out.

                                • Re: Solidworks 2017 Selection Macro Error
                                  Josh Brady

                                  Solid Air wrote:

                                   

                                  Josh,

                                  Thanks for the clarification. I thought the user selected two components before running the macro. And now I understand why the macro operates on the last selected component (so you can use the control key). It also explains the results when I select different items.

                                   

                                  As I was writing this, I selected a plane in the top level assembly; I got the run-time error 91 message. Could this be Orb's issue? Are you supposed to be able to select something in the top level assembly? I will run the macro in previous SW versions to see if it errors out.

                                  Hmm, I had never even considered selecting a feature of the top level assembly.  I do get that error.  However, the assembly doesn't seem to become "toxic" as OP mentioned.  At any rate, that error can be fixed by replacing the single offending line with:

                                   

                                      If swSelComp Is Nothing Then

                                          Set swSelComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(False)

                                          Set swFeat = swModel.FirstFeature

                                      Else

                                          Set swFeat = swSelComp.FirstFeature

                                      End If

                                   

                                  It then works just fine on top-level selections as well, as far as I can tell.

                                   

                                  Now that I've thought about it a little bit, why should the macro do nothing when nothing is selected?  How about going ahead and selecting the top level's reference geometry?  With another small tweak, that can be achieved:

                                   

                                  '-------------------------------------------------
                                  
                                  
                                  ' Macro to select main reference geometry of an assembly component
                                  ' for easy mating.  Modified extensively from the
                                  ' "Select Origin of Assembly Component Example (VB)" in
                                  ' SolidWorks and Add-Ins API Help
                                  
                                  
                                  ' Preconditions:
                                  
                                  
                                  '       (1) Assembly document is open.
                                  
                                  
                                  '       (2) One or more items is selected.
                                  
                                  
                                  '
                                  
                                  
                                  ' Postconditions: One of the reference planes or the
                                  ' origin of the last selected component is selected.
                                  
                                  
                                  '
                                  
                                  
                                  '--------------------------------------------------
                                  
                                  
                                  Const STOPATORIGIN As Boolean = False
                                  Const FIRSTREF As Long = 1
                                  'Change the value of FIRSTREF above if you want
                                  'one of the primary planes to be the first feature
                                  'selected by the macro.  Values are:
                                      'Front  = 1
                                      'Top    = 2
                                      'Right  = 3
                                      'Origin = 4
                                  '''''''''
                                  
                                  
                                  Option Explicit
                                  
                                  
                                  Sub main()
                                  
                                  
                                      Dim swApp                       As SldWorks.SldWorks
                                      Dim swModel                     As SldWorks.ModelDoc2
                                      Dim swSelMgr                    As SldWorks.SelectionMgr
                                      Dim swSelComp                   As SldWorks.Component2
                                      Dim swCompModel                 As SldWorks.ModelDoc2
                                      Dim swFeat                      As SldWorks.Feature
                                      Dim bRet                        As Boolean
                                      Dim GeneralSelObj               As Object
                                      Dim myFeatureCollection         As New Collection
                                      Dim i                           As Integer
                                      Dim CurSelCount                 As Long
                                      Dim MyTempPointObj              As Object
                                      Dim mySelStr                    As String
                                      Dim NewObjToSelect              As Object
                                      Dim Chunks                      As Variant
                                      Dim swVer                       As Variant
                                      Dim ResolveIt                   As Integer
                                      Dim DocTitle                    As String
                                  
                                  
                                      Set swApp = Application.SldWorks
                                      Set swModel = swApp.ActiveDoc
                                      
                                      If swModel.GetType <> swDocASSEMBLY Then
                                          MsgBox "This macro works on assembly documents only."
                                          Exit Sub
                                      End If
                                      
                                      Set swSelMgr = swModel.SelectionManager
                                      CurSelCount = swSelMgr.GetSelectedObjectCount
                                      If CurSelCount <> 0 Then
                                      
                                          Set GeneralSelObj = swSelMgr.GetSelectedObject(CurSelCount)
                                          Set swSelComp = swSelMgr.GetSelectedObjectsComponent(CurSelCount)
                                          swVer = Split(swApp.RevisionNumber, ".")
                                          If CInt(swVer(0)) < 14 Then
                                              If swSelComp.GetSuppression <> swComponentFullyResolved Then
                                                  If swSelComp.GetSuppression <> swComponentResolved Then
                                                      ResolveIt = MsgBox("The component selected is not fully resolved." _
                                                         & vbCrLf & "This functionality is only available for lightweight" & vbCrLf & _
                                                         "components in SolidWorks 2006 or greater." & vbCrLf & vbCrLf & _
                                                         "Resolve this component now?", vbYesNo, "Upgrade Time!")
                                                      If vbYes = ResolveIt Then
                                                          swSelComp.SetSuppression2 swComponentFullyResolved
                                                      Else
                                                          Exit Sub
                                                      End If
                                                  End If
                                              End If
                                          End If
                                          swSelMgr.DeSelect CurSelCount
                                      End If
                                  
                                  
                                      
                                  
                                  
                                      If swSelComp Is Nothing Then
                                          Set swSelComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(False)
                                          Set swFeat = swModel.FirstFeature
                                      Else
                                          Set swFeat = swSelComp.FirstFeature
                                      End If
                                      
                                      Do While Not swFeat Is Nothing
                                  
                                  
                                          If "RefPlane" = swFeat.GetTypeName Then
                                               myFeatureCollection.Add swFeat
                                          End If
                                              
                                          If "OriginProfileFeature" = swFeat.GetTypeName Then
                                              Chunks = Split(swSelComp.Name2, "/")
                                              If StrComp(Right(swModel.GetTitle, 7), ".sldasm", vbTextCompare) <> 0 Then
                                                  DocTitle = swModel.GetTitle
                                              Else
                                                  DocTitle = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)
                                              End If
                                              mySelStr = "Point1@Origin@" & Chunks(0) & "@" & DocTitle
                                              For i = 0 To (UBound(Chunks) - 1)
                                                  mySelStr = mySelStr & "/" & Chunks(i + 1) & "@" & Left(Chunks(i), (InStrRev(Chunks(i), "-") - 1))
                                              Next
                                              swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _
                                                  0, 0, 0, True, 0, Nothing, swSelectOptionDefault
                                              myFeatureCollection.Add swSelMgr.GetSelectedObject(swSelMgr.GetSelectedObjectCount)
                                              swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _
                                                  0, 0, 0, True, 0, Nothing, swSelectOptionDefault
                                              If STOPATORIGIN Then
                                                  Exit Do
                                              End If
                                          End If
                                  
                                  
                                          Set swFeat = swFeat.GetNextFeature
                                  
                                  
                                      Loop
                                  
                                  
                                      Set NewObjToSelect = Nothing
                                      For i = 1 To myFeatureCollection.Count
                                      
                                          If GeneralSelObj Is myFeatureCollection.Item(i) Then
                                              Set NewObjToSelect = myFeatureCollection.Item((i Mod myFeatureCollection.Count) + 1)
                                              'Use of Mod above cycles back to first item if last item matches.
                                          End If
                                          
                                      Next
                                      
                                      If NewObjToSelect Is Nothing Then
                                          Set NewObjToSelect = myFeatureCollection.Item(1)
                                          On Error Resume Next
                                          Set NewObjToSelect = myFeatureCollection.Item(FIRSTREF)
                                          On Error GoTo 0
                                      End If
                                  
                                  
                                      bRet = NewObjToSelect.Select(True): Debug.Assert bRet
                                  
                                  
                                  End Sub
                                  
                                  
                                  '-------------------------------------------------
                                  
                                    • Re: Solidworks 2017 Selection Macro Error
                                      Solid Air

                                      Sounds like a good idea.

                                       

                                      Something else you could do is temporarily put in on error resume next statement, check if swFeat is nothing and if it is, debug.print the name of the selected object so you can figure out what is causing the error the first time.

                                        • Re: Solidworks 2017 Selection Macro Error
                                          Josh Brady

                                          Solid Air wrote:

                                           

                                          Sounds like a good idea.

                                           

                                          Something else you could do is temporarily put in on error resume next statement, check if swFeat is nothing and if it is, debug.print the name of the selected object so you can figure out what is causing the error the first time.

                                          The cause of the runtime error that you saw isn't that swFeat is nothing... It's that swSelComp is Nothing and raises the error when you try to get the FirstFeature of Nothing.  When you select a feature of the top level assembly, swSelComp is Nothing because GetSelectedObjectsComponent returned Nothing because you haven't selected a component.  Therefore to get the top level's features, you have to get FirstFeature from the top level ModelDoc2 instead.  That's what the little replacement block of code does.

                                            • Re: Solidworks 2017 Selection Macro Error
                                              Josh Brady

                                              I suppose I should have checked whether the posted macro actually matches my code.  I was just looking at my original one.  I looked again at the posted code and see that at some point someone has attempted to add a check for suppressed reference geometry to skip it.  That check was not implemented in the best way, although I don't see any way it could have caused errors either.  That said, I incorporated the suppression check properly into the macro posted below.

                                               

                                              I also added another check... Previously the macro would error if the user selects a suppressed component in the feature tree and runs the macro.  I can't imagine why one would do that, given the intended function of the macro, but hey.  Error trapped and accounted for.

                                               

                                              '-------------------------------------------------
                                              ' Macro to select main reference geometry of an assembly component
                                              ' for easy mating.  Modified extensively from the
                                              ' "Select Origin of Assembly Component Example (VB)" in
                                              ' SolidWorks and Add-Ins API Help
                                              ' Preconditions:
                                              '       (1) Assembly document is open.
                                              ' Postconditions: One of the reference planes or the
                                              ' origin of the last selected component is selected.
                                              ' Or, if nothing was selected, reference geometry of the
                                              ' top level assembly.
                                              '--------------------------------------------------
                                                
                                              Const STOPATORIGIN As Boolean = False
                                              Const FIRSTREF As Long = 1
                                              'Change the value of FIRSTREF above if you want
                                              'one of the primary planes to be the first feature
                                              'selected by the macro.  Values are:
                                                  'Front  = 1
                                                  'Top    = 2
                                                  'Right  = 3
                                                  'Origin = 4
                                              '''''''''
                                              
                                              
                                              Option Explicit
                                                
                                              Sub main()
                                                  Dim swApp                       As SldWorks.SldWorks
                                                  Dim swModel                     As SldWorks.ModelDoc2
                                                  Dim swSelMgr                    As SldWorks.SelectionMgr
                                                  Dim swSelComp                   As SldWorks.Component2
                                                  Dim swCompModel                 As SldWorks.ModelDoc2
                                                  Dim swFeat                      As SldWorks.Feature
                                                  Dim bRet                        As Boolean
                                                  Dim GeneralSelObj               As Object
                                                  Dim myFeatureCollection         As New Collection
                                                  Dim i                           As Integer
                                                  Dim CurSelCount                 As Long
                                                  Dim MyTempPointObj              As Object
                                                  Dim mySelStr                    As String
                                                  Dim NewObjToSelect              As Object
                                                  Dim Chunks                      As Variant
                                                  Dim swVer                       As Variant
                                                  Dim ResolveIt                   As Integer
                                                  Dim DocTitle                    As String
                                                
                                                  Set swApp = Application.SldWorks
                                                  Set swModel = swApp.ActiveDoc
                                                    
                                                  If swModel.GetType <> swDocASSEMBLY Then
                                                      MsgBox "This macro works on assembly documents only."
                                                      Exit Sub
                                                  End If
                                                    
                                                  Set swSelMgr = swModel.SelectionManager
                                                  CurSelCount = swSelMgr.GetSelectedObjectCount
                                                  If CurSelCount <> 0 Then
                                                      Set GeneralSelObj = swSelMgr.GetSelectedObject(CurSelCount)
                                                      Set swSelComp = swSelMgr.GetSelectedObjectsComponent(CurSelCount)
                                                      If Not swSelComp Is Nothing Then
                                                          If swSelComp.GetSuppression = swComponentSuppressed Then
                                                              MsgBox "Can't get to reference geometry of a suppressed component."
                                                              Exit Sub
                                                          End If
                                                      End If
                                                      swVer = Split(swApp.RevisionNumber, ".")
                                                      If CInt(swVer(0)) < 14 Then
                                                          If swSelComp.GetSuppression <> swComponentFullyResolved Then
                                                              If swSelComp.GetSuppression <> swComponentResolved Then
                                                                  ResolveIt = MsgBox("The component selected is not fully resolved." _
                                                                     & vbCrLf & "This functionality is only available for lightweight" & vbCrLf & _
                                                                     "components in SolidWorks 2006 or greater." & vbCrLf & vbCrLf & _
                                                                     "Resolve this component now?", vbYesNo, "Upgrade Time!")
                                                                  If vbYes = ResolveIt Then
                                                                      swSelComp.SetSuppression2 swComponentFullyResolved
                                                                  Else
                                                                      Exit Sub
                                                                  End If
                                                              End If
                                                          End If
                                                      End If
                                                      swSelMgr.DeSelect CurSelCount
                                                  End If
                                                
                                                  If swSelComp Is Nothing Then
                                                      Set swSelComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(False)
                                                      Set swFeat = swModel.FirstFeature
                                                  Else
                                                      Set swFeat = swSelComp.FirstFeature
                                                  End If
                                                    
                                                  Do While Not swFeat Is Nothing
                                                      If ("RefPlane" = swFeat.GetTypeName) And (False = swFeat.IsSuppressed) Then
                                                           myFeatureCollection.Add swFeat
                                                      End If
                                                            
                                                      If "OriginProfileFeature" = swFeat.GetTypeName Then
                                                          Chunks = Split(swSelComp.Name2, "/")
                                                          If StrComp(Right(swModel.GetTitle, 7), ".sldasm", vbTextCompare) <> 0 Then
                                                              DocTitle = swModel.GetTitle
                                                          Else
                                                              DocTitle = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)
                                                          End If
                                                          mySelStr = "Point1@Origin@" & Chunks(0) & "@" & DocTitle
                                                          For i = 0 To (UBound(Chunks) - 1)
                                                              mySelStr = mySelStr & "/" & Chunks(i + 1) & "@" & Left(Chunks(i), (InStrRev(Chunks(i), "-") - 1))
                                                          Next
                                                          swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _
                                                              0, 0, 0, True, 0, Nothing, swSelectOptionDefault
                                                          myFeatureCollection.Add swSelMgr.GetSelectedObject(swSelMgr.GetSelectedObjectCount)
                                                          swModel.Extension.SelectByID2 mySelStr, "EXTSKETCHPOINT", _
                                                              0, 0, 0, True, 0, Nothing, swSelectOptionDefault
                                                          If STOPATORIGIN Then
                                                              Exit Do
                                                          End If
                                                      End If
                                                      Set swFeat = swFeat.GetNextFeature
                                                  Loop
                                                
                                                  Set NewObjToSelect = Nothing
                                                  For i = 1 To myFeatureCollection.Count
                                                      If GeneralSelObj Is myFeatureCollection.Item(i) Then
                                                          Set NewObjToSelect = myFeatureCollection.Item((i Mod myFeatureCollection.Count) + 1)
                                                          'Use of Mod above cycles back to first item if last item matches.
                                                      End If
                                                  Next
                                                    
                                                  If NewObjToSelect Is Nothing Then
                                                      Set NewObjToSelect = myFeatureCollection.Item(1)
                                                      On Error Resume Next
                                                      Set NewObjToSelect = myFeatureCollection.Item(FIRSTREF)
                                                      On Error GoTo 0
                                                  End If
                                                  
                                                  bRet = NewObjToSelect.Select(True): Debug.Assert bRet
                                                
                                              End Sub
                                              '-------------------------------------------------
                                              
                                              • Re: Solidworks 2017 Selection Macro Error
                                                Solid Air

                                                Josh,

                                                I agree that the cause is most likely the swSelComp is nothing.  I thought of that this morning and was going to suggest a different course of action because is it a given that the reason for the error is that the user selected something in the top level assembly?  In the original post, the user states that the issue did not start until upgrading to SW2017.  My experimentation in SW2012 and SW2016 found the error occurred when selecting any feature in the top level assembly.  Since the user seems to have used the macro for awhile, I have to assume that the user would have gotten the error in the previous version also by selecting something in the top level assembly.

                                                 

                                                My new suggestion is still use the on error resume next but instead of checking for an object = nothing, check the err.number to see if an error occurred then check what was selected.  In SW2017, it could be possible that selecting something in a sub-assembly does not return a component object.

                                                  • Re: Solidworks 2017 Selection Macro Error
                                                    Josh Brady

                                                    In SW2017, it could be possible that selecting something in a sub-assembly does not return a component object.

                                                    No, I already checked that.  As I mentioned earlier, I use this macro daily in 2017 at multiple levels of subassemblies.  I even tried it out while editing subassemblies in context, selecting components from all levels.  Still no errors.

                                          • Re: Solidworks 2017 Selection Macro Error
                                            Solid Air

                                            Ran the macro on a SW2012 assembly and got the same error 91 when I selected a plane in the top level assembly.  I do not have a SW2017 assembly with sub-assemblies in it so guess I will have to create one (tomorrow's project unless you or someone else comes up with the solution).

                                        • Re: Solidworks 2017 Selection Macro Error
                                          Orb D.

                                          Hey guys, sorry, I left for the day before your responses. Yes, the error you experience seems to be the same as mine. It starts to select the top levels origin and then errors out. For me, none of the parts in that assembly can use the macro again, so it is "toxic" I suppose, to that assembly. I will be trying your latest revised macro today and see how it works. Thanks a ton

                                          • Re: Solidworks 2017 Selection Macro Error
                                            Orb D.

                                            I just tried the latest macro posted on an assembly that had error'd with the original macro. It cycles the first time through the planes now but reverts back to the high level assemblies planes, and loops those. (instead of looping the selected parts features)