4 Replies Latest reply on Mar 19, 2018 4:56 PM by John Lega

    Change Selected Objects To Another Layer

    John Lega

      I would like to be able to select objects in a drawing (any kind of objects, dimensions, notes, sketches, etc) and move them to a new layer. I am using the following macro at the moment:


      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swSelObj As Object

      Dim swAnn As SldWorks.Annotation

      Dim i As Integer


      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

              Set swSelObj = swSelMgr.GetSelectedObject6(i, -1)

             Set swAnn = swSelObj.GetAnnotation

              If Not swAnn Is Nothing Then swAnn.Layer = "DIM"

          Next i


      End Sub


      I'm getting runtime error 438 when, for example, I pick a sketch entity to change layer. I think the issue is that it is expecting an annotation. What do I need to do to the code to allow me to select ANYTHING on a drawing and change it's layer?


      Thanks all... sorry for dumb question - I really need to learn to code.....