6 Replies Latest reply on Sep 20, 2018 7:04 AM by Niels Raahauge

    CloseAllDocuments but NOT the active model

    Niels Raahauge

      Sometimes a SWA open a lot of parts. I would like to close them by a macro.

      If the SWA is edit, I can use  boolstatus = swApp.CloseAllDocuments(False)

      The active document stay open as wanted.


      I would like to close all but not the active SWA - without edit it.

      I have tried to add a props in the active model, and delete it again after close all - but i does not work.

      If I change add the props outside the macro - it is detected as a changes - and it stays open after close All.


      It seems that solidworks not detect a props change as a change in the document - the document can be closed without prompt for save if this command are used:

      AddStatus = CustPropMgr.Add3("TempProps", swCustomInfoText, " ", 0)


      'open some documents

      'add temp props

      'close all -not edit- documents

      'delete the temp props

      'Does not work, because a props change - are not detected as a document change


      Option Explicit


      Dim swApp As SldWorks.SldWorks    ' Define variable used to hold the SldWorks object

      Dim swModel As SldWorks.ModelDoc2 ' Define variable used to hold the ModelDoc object

      Dim boolstatus As Boolean

      Dim result As Integer


      Dim swCustPropMgr As SldWorks.CustomPropertyManager

      Sub main()


      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc ' Grab the currently active document


      'make a document edit-to keep document open

      Dim CustPropMgr As SldWorks.ICustomPropertyManager

      Set CustPropMgr = swModel.Extension.CustomPropertyManager("")

      Dim AddStatus As Long

      AddStatus = CustPropMgr.Add3("TempProps", swCustomInfoText, " ", 0)


      result = MsgBox("Close all documents that have no changes?", vbYesNo, "Solidworks")


      Select Case result

      Case vbYes

          ' If false, then all documents, excluding any dirty documents, are closed

          boolstatus = swApp.CloseAllDocuments(False)

          Debug.Print "All documents, excluding dirty documents, closed: " & boolstatus

          ' If true, then all documents, including any dirty documents, are closed

          'boolstatus = swApp.CloseAllDocuments(True)

          'Debug.Print "All documents, including dirty documents, closed: " & boolstatus

      Case vbNo


      End Select


      Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)

      swCustPropMgr.Delete "TempProps"


      End Sub