AnsweredAssumed Answered

VBA Macro: save selected body as STL

Question asked by Van Maele Thomas on Apr 27, 2018
Latest reply on Oct 22, 2018 by Arpad Kormendy



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,