Organize Usings Across Your Entire Solution

One of the features I worked on for the Visual Studio 2008 release was the new "Organize Usings" feature.  This feature allows you to:

  1. Remove Unused Usings - Determines which using directives are not used in the current file and deletes them.   
  2. Sort Usings - Sorts the using directives in a file

We've received a great response from customers on the usefulness of this feature but one of the questions that I've received quite often is - "is there a way to make the feature work on an entire project or solution"?  The current answer to this is no.  Given the number of times I've heard the request though, I thought it would be worth posting a workaround that uses macros. 

So here's what you need to do - it's really quite simple:

Step 1: Create a new macro in Visual Studio through the Tools | Macros menu.

Step 2: Paste the code below into the Module and save it

Note that the code below is courtesy of Kevin Pilch-Bisson ( and Chris Eargle (, who have posted macros that allow you to format across an entire solution.  I'm simply re-applying their approach to invoke the RemoveAndSort command rather than the FormatDocument command.  Note that I haven't tested this macro extensively so please use at your own risk. 

Public Module Module1
    Sub OrganizeSolution()
        Dim sol As Solution = DTE.Solution
        For i As Integer = 1 To sol.Projects.Count
    End Sub
    Private Sub OrganizeProject(ByVal proj As Project)
        For i As Integer = 1 To proj.ProjectItems.Count
    End Sub
    Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem)
        Dim fileIsOpen As Boolean = False
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            'If this is a c# file 
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                'Set flag to true if file is already open 
                fileIsOpen = projectItem.IsOpen
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                'Only close the file if it was not already open 
                If Not fileIsOpen Then
                End If
            End If
        End If
        'Be sure to apply RemoveAndSort on all of the ProjectItems. 
        If Not projectItem.ProjectItems Is Nothing Then
            For i As Integer = 1 To projectItem.ProjectItems.Count
        End If
        'Apply RemoveAndSort on a SubProject if it exists. 
        If Not projectItem.SubProject Is Nothing Then
        End If
    End Sub
End Module

Step 3: Run the macro on any solution that you'd like and there you have it!  Enjoy 🙂

Comments (23)
  1. You’ve been kicked (a good thing) – Trackback from

  2. This is also available in the VS 2008 Power Commands addin,

    But that’s cool to see how easy it is implement with the use of a macro.  Anyway, I didn’t know you were the guy you worked on it.  Really awesome feature DJ!

  3. Chad Lee says:

    This looks great! However, when I try to run it on one of my larger solutions, it works for a while and then eventually fails with the error:

    The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_SERVERCALL_RETRYLATER))

    Any ideas how to get this to work with larger solutions?

  4. Brian says:

    Excellent, great for tidying up after I’ve tried 10 different ways to solve a problem …

  5. Francois Nel says:

    Works a charm in a test solution. Will test it on a large solution soon.

  6. Applied for a large solution. Great work done. Thanks.

  7. Francois Nel says:

    Also got an error when running it on a large project:

    System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED))

  8. TechNeilogy says:

    I’m current doing a code review and cleanup so I will try this as part of that effort.

    To be honest, to this point I had completely ignored the "Organize Usings" menu item.  To bad for me; it’s really handy!  I don’t like unused code of any kind, so this is the kind of simple feature that gets me through a project with a lot less stress.


  9. chau says:

    Is there a way in VS.NET to order the fields and properties and methods automatically? and put them into #region blocks? or have a macro of some tool? Know of any?

  10. A short while ago I wrote an add-in for the same purpose that you can find here:

  11. Links of the Week #50 (week 34/2008)

  12. Zunanji viri says:

    Development Clone Detective for Visual Studio – "Clone Detective is a Visual Studio integration

  13. Organize Usings Across Your Entire Solution

  14. My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Sara Ford’s Tip of the Day #303 covers the QuickWatch window . Carlos Quintero posted The diagram of the convoluted build configuration

  15. Govt says:

    It would be cool if there was an a capability of removing any unnecessary references too.

  16. Frank says:


    Probably not what you want to hear, but (third party)  tools like ReSharper can do this also, as wel as removing unused references.

    Maybe you can look at some of its features while developing a new version of VS?

  17. tcmaster says:

    Has anyone heard about a product named R#?

  18. Tuubabm says:

    Why i can’t read .aspx source code if it has .cs file?

    Hi, i have a problem about reading .aspx file. If projectItem ends with .aspx and it has .aspx.cs when my .aspx window is active it reads the .cs code that it has. But if my item hasn’t a .cs file it has only .aspx file it reads the source code of aspx it works correctly.How Can I solve this problem? Here is my code :

    Private Sub ReadCodeFile(ByVal projectItem As EnvDTE.ProjectItem)

           ‘ Read file Content

           Dim codeWindow As Window = projectItem.Open(Constants.vsViewKindCode)


           ‘Load text into text buffer

           Dim objTextDoc As EnvDTE.TextDocument = codeWindow.Document.Object()

           ‘Manipulate text as data in text buffers

           Dim objEditPt As EditPoint = objTextDoc.StartPoint.CreateEditPoint()

           ‘Move the object to the beginning of the document


           ‘Returns the text between the current location and the specified location in the buffer

           content = objEditPt.GetText(objTextDoc.EndPoint)


       End Sub

    please alert me by my email

  19. Michel Schep says:

    The functionalitity to remove redundant usings in the entire solution is part of the ReSharper add on for a long time.

  20. Rick O'Shay says:

    In addition to fixing this in the next release, how about a process whereby such obvious needs aren’t missed going forward? There is something wrong with the process when something so obvious falls through the cracks.

  21. Timm says:

    Awesome macro, but it halts when it encounters a C# source file with a "Build Action" set to None.  This is because the Intellisense > Organize Usings menu is not available for files with BuildAction=None.  I’ve Googled for a solution but couldn’t find a way to poll the projectItem for BuildAction.  Any ideas?  Thanks.

  22. John says:

    I've been trying for a while make this work on VS2010. Have you tried it? It opens the document, closes but the actual DTE.ExecuteCommand doesn't work. Tried also with DTE.ExecuteCommand("Edit.RemoveUnusedUsings").

Comments are closed.

Skip to main content