Share via


macro to "organize usings" for a project

I won't claim this is efficient, or well-written, or whatever, but it WorksForMe and I figured I'd share.  Feel free to post better versions in the comments or on your own blog and add a link as a comment :)

 Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module RemoveAndSortModule
    Sub RemoveAndSortAll()
        IterateFiles()
    End Sub

    Private Sub IterateFiles()
        Dim project As Project
        Dim projectObjects As Object()
        Dim window As Window
        Dim target As Object

        window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
        projectObjects = DTE.ActiveSolutionProjects
        If projectObjects.Length = 0 Then
            Exit Sub
        End If
        project = DTE.ActiveSolutionProjects(0)
        RemoveAndSortFiles(project.ProjectItems())
    End Sub

    Private Sub RemoveAndSortFiles(ByVal items)
        Dim file As ProjectItem
        For Each file In items
            DTE.ExecuteCommand("View.SolutionExplorer")
            If file.Name.EndsWith(".cs") Then
                file.Open()
                file.Document.Activate()
                DTE.ExecuteCommand("View.ViewCode")

                DTE.ExecuteCommand("Edit.RemoveAndSort")

                file.Document.Save()
                file.Document.Close()
            End If
            If file.ProjectItems().Count > 0 Then
                RemoveAndSortFiles(file.ProjectItems())
            End If
        Next
    End Sub
End Module

Comments