API: Close all open documents

Jun 18, 2004 0 Replies

I had to close all the open documents in an application I was writing. I ended up having to work this out, so thought I would share it. It's VB.NET, but could be easily modified to work in a macro or VB6.



Private Function CloseAllOpenSWFiles()



'close all open docs



Dim OpenDocs As SldWorks.EnumDocuments2



Dim OpenDoc As SldWorks.ModelDoc2



OpenDocs = swApp.EnumDocuments2()



OpenDoc = swApp.ActiveDoc



While Not OpenDoc Is Nothing



Try



OpenDoc.Save3(SwConst.swSaveAsOptions_e.swSaveAsOptions_SaveReferenced, 0,



0)

Debug.WriteLine("Closing " & OpenDoc.GetPathName)



swApp.CloseDoc(OpenDoc.GetPathName)



OpenDocs.Next(1, OpenDoc, 0)



Catch ex As Exception



'if errors out, then all documents have been closed



OpenDoc = Nothing



End Try



End While



End Function



Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required