This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This macro utilizes SOLIDWORKS API and closes all opened documents except of an active one.
If document is dirty (i.e. has any unsaved changes) the macro will prompt user to specify the action (save, do not save or cancel) for the closing documents. Otherwise the document will be closed silently.
#If VBA7 ThenPrivateDeclare PtrSafe Function SendMessage Lib"User32"Alias"SendMessageA" (ByVal hWnd AsLong, ByVal wMsg AsLong, ByVal wParam AsLong, lParam AsAny) AsLong#ElsePrivateDeclareFunction SendMessage Lib"User32"Alias"SendMessageA" (ByVal hWnd AsLong, ByVal wMsg AsLong, ByVal wParam AsLong, lParam AsAny) AsLong#End IfDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenDim vDocsWindows AsVariantDim swFrame As SldWorks.Frame
Set swFrame = swApp.Frame
vDocsWindows = swFrame.ModelWindows
Dim i AsIntegerFor i = 0 To UBound(vDocsWindows)
Dim swDocWin As SldWorks.ModelWindow
Set swDocWin = vDocsWindows(i)
Dim swRefDoc As SldWorks.ModelDoc2
Set swRefDoc = swDocWin.ModelDoc
IfNot swRefDoc Is swModel ThenIf swRefDoc.GetSaveFlag() Then'display the close confirmation dialog for unsaved files
swApp.ActivateDoc3 swRefDoc.GetTitle, False, swRebuildOnActivation_e.swDontRebuildActiveDoc, 0
Const WM_COMMAND AsLong = &H111
Const CMD_FileClose AsLong = 57602
SendMessage swFrame.GetHWnd(), WM_COMMAND, CMD_FileClose, 0
Else
swApp.CloseDoc swDocWin.ModelDoc.GetTitle
EndIfEndIfNext
swApp.ActivateDoc3 swModel.GetTitle, True, swRebuildOnActivation_e.swUserDecision, 0
EndIfEndSub
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more