KnowDotNet

Close All Windows but the Current Window in the Visual Studio .NET IDE

by Les Smith

How can I close all windows in the IDE except the one in which I am currently working?  I have a number of windows open in the IDE and I would like to be able to close all windows in the IDE except the one in which I am currently working.   I need to do this so that I can open another window and work in the currently active window and a new one without the IDE looking so cluttered.  

The code shown below will close all but the current window (and Start Page if open).   The code is set up to run in an add-in.  You can use it in a macro by simply changing every reference to "oVS" to "DTE".  "oVS" is a reference to the applicationObject in the IDE.


    Public Sub CloseAllButCurrentWindow()
        
Dim i As Integer
        Dim sCurrWin As String = oVS.ActiveDocument.Name

        
With oVS
            
On Error Resume Next
            For i = .Documents.Count To 1 Step -1
                
If .Documents.Item(i).Name <> sCurrWin Then
                    If Not .Documents.Item(i).Saved Then
                        .Documents.Item(i). _
                          Close(vsSaveChanges.vsSaveChangesYes)
                    
Else
                        .Documents.Item(i). _
                           Close(vsSaveChanges.vsSaveChangesNo)
                    
End If
                End If
            Next
        End With
    End Sub

Back to Top