6 Replies Latest reply on Jan 30, 2017 3:11 PM by Kevin Collins

    API Saving - existing part vs new

    Kevin Collins

      I am hoping some of you genius's can help me.


      I have a Macro written that runs some code to update configurations and at the end of the code, saves the part.


      It works great with the exception of one thing.  If my save code reads as follows:


      boolstatus = swModel.Save3(swSaveAsOptions_Silent, IErrors, IWarnings)


      ...then Solidworks will not save the part if the part is new.


      If I change it to:


      boolstatus = swModel.Save3(swSaveAsOptions_SaveReferenced, IErrors, IWarnings)


      ... then it prompts every single part that is read.only to save.


      I have tried the following:


           If swModel.IsOpenedReadOnly Then

               boolstatus = swModel.Save3(swSaveAsOptions_Silent, IErrors, IWarnings)


               boolstatus = swModel.Save3(swSaveAsOptions_SaveReferenced, IErrors, IWarnings)

               End If

      That reduced the number of prompts but did not fix everything.


      My question is, can this If / Then statement be re-written to say If the part is New, Save Referenced, Else, Save Silent?  The "If the part is new" is the item I'm struggling with as I can't seem to find the code for it.



        • Re: API Saving - existing part vs new
          Raghvendra Bhargava

          Hi Kevin,

          Save3 will only work if Active Part is already saved. We need to use SaveAs for saving new Part.

          Try this:


          Option Explicit


          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim boolstatus As Boolean

          Dim IError As Long

          Dim IWarning As Long


          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc


              boolstatus = swModel.Save3(swSaveAsOptions_Silent, IError, IWarning)

              If boolstatus <> True And IError <> 0 Then boolstatus = swModel.Extension.SaveAs(swApp.GetCurrentMacroPathFolder & "\test.sldprt", 0, 1, Nothing, IError, IWarning)

              Set swSelMgr = swModel.SelectionManager


          End Sub

          • Re: API Saving - existing part vs new
            Nilesh Patel

            Hi Kevin,


            Try following codes:


            If swModel.GetPathName = "" Then
                    If Not swApp.RunCommand(swCommands_e.swCommands_SaveAs, Empty) Then
                        swApp.SendMsgToUser2 "Error: Failed to run 'Save AS' command.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                    End If
                    If Not swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, lError, lWarning) Then
                        swApp.SendMsgToUser2 "Error: Failed to save the document '" & swModel.GetTitle & "'.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                    End If
                End If


            It will open native SolidWorks 'Save As' dialogue box for new parts whereas saves the existing part. This way you can choose a document name and location for new part.


            Hope this helps.