1 Reply Latest reply on Apr 22, 2017 10:08 AM by Peter Brinkhuis

    Sort BOM VBA Macro issues

    Cad Admin

      I have a macro, that i thought was correct... I want to sort my top level BOMs by "Part number", "ascending" in order of (1) assemblies then (2)parts...

      The normal "Sort" GUI works, must be something i cant see...



      Dim swApp        As SldWorks.SldWorks
      Dim swModel      As SldWorks.ModelDoc2
      Dim swDraw       As SldWorks.DrawingDoc
      Dim swBomTable   As SldWorks.BomTableAnnotation
      Dim swBomFeat    As SldWorks.BomFeature
      Dim swSortData   As SldWorks.BomTableSortData
      Dim swSelMgr     As SldWorks.SelectionMgr
      Dim swFeat       As SldWorks.Feature
      Dim sortArray(2) As String
      Dim vTables      As Variant
      Dim boolstatus   As Boolean


      Sub Sort_BOM_Data()


      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swDraw = swModel

      Set swSelMgr = swModel.SelectionManager

      Set swFeat = swModel.FirstFeature


      Do While Not swFeat Is Nothing

      If "BomFeat" = swFeat.GetTypeName Then
          swFeat.Select (False)
          Set swBomFeat = swFeat.GetSpecificFeature2
          vTables = swBomFeat.GetTableAnnotations
          Set swBomTable = vTables(0)
      End If
      Set swFeat = swFeat.GetNextFeature



      Set swSortData = swBomTable.GetBomTableSortData


      swSortData.ColumnIndex(0) = 3

      swSortData.Ascending(0) = True


      sortArray(0) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Assemblies

      sortArray(1) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Parts

      sortArray(2) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_None


      swSortData.ItemGroups = sortArray

      swSortData.DoNotChangeItemNumber = False


      boolstatus = swBomTable.Sort(swSortData)


      swModel.ClearSelection2 (True)


      End Sub