KnowDotNet

Closing All Saved Windows in the IDE

by Les Smith

The Visual Studio .NET IDE has a menu item for closing all open windows in the IDE, but I want to close all windows except those that I have made changes to and have not yet saved.  I may want to take one more look at the changes that I have made and only want to know which windows I have not yet saved.  The code shown below will close all windows that have no unsaved changes and leave the "dirty" windows open.  The code is for an add-in but can be modified to run in a macro by changing oVB to DTE.



    Public Sub CloseAllSavedWindows()
        
' Close all saved documents.
        Dim i As Integer
        
        
' oVB is the ApplicationObject (ptr to IDE)
        With oVB
            
On Error Resume Next
            For i = .Documents.Count To 1 Step -1
                
If .Documents.Item(i).Saved Then
                    .Documents.Item(i).Close()
                
End If
            Next i
        
End With
    End Sub