11 Replies Latest reply on Oct 26, 2018 5:26 AM by Arpad Kormendy

    VBA Macro: save selected body as STL

    Van Maele Thomas



      The Macro below saves the selected body (of a part) into a STL file.

      I have been using it in SW2017 on a daily basis, but after upgrading to SW2018 it stopped working...

      It now saves an empty STL file of 1 kB. Does anybody know what goes wrong?


      Option Explicit

      Sub main()


      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swBody As SldWorks.Body2


      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swSelMgr = swModel.SelectionManager

      Set swBody = swSelMgr.GetSelectedObject6(1, -1)


      Dim FilePath As String

      Dim NewFilePath As String

      Dim ConfigName As String

      Dim BodyName As String


      'Save the active model

      swModel.Save3 0, 0, 0


      'Check if the the active model is a PART  and if there is a solid body selected

      If swModel.GetType = swDocPART Then

          If swBody Is Nothing Then

              MsgBox "Please select a solid Body"


              FilePath = swModel.GetPathName

              ConfigName = swApp.GetActiveConfigurationName(FilePath)

              BodyName = swBody.Name


              NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & ConfigName & " [" & BodyName & "]" & ".STL"


              swModel.SaveAs2 NewFilePath, 0, True, False


              MsgBox "van Cronenburg 3D Print STL saved." & vbNewLine & vbNewLine &  "Filename: " & ConfigName & " [" & BodyName & "]." & "STL"

          End If

      End If


      End Sub


      Kind regards,