Sometimes you want to find the current file in the tree though, so...
Here is a macro to find the active file in solution explorer.
[updated for vs2005 with changes from Simon R.]
Sub FindFileInSolutionExplorer()Coupled with the ever useful "Collapse all the open project folders in the Solution Explorer".
Dim sln As String = DTE.Solution.FullName
sln = sln.Substring(sln.LastIndexOf("\") + 1)
sln = sln.Substring(0, sln.Length - 4)
Dim pi As Object = DTE.ActiveDocument.ProjectItem
Dim endString As String = "Microsoft Visual Studio"
Dim path As String
While (Not endString = pi.Name)
path = "\" & pi.Name() & path
pi = pi.Collection.Parent
End Whilejavascript:void(0)
path = sln & path
Dim UIH As UIHierarchy =
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object
Dim anItem As UIHierarchyItem = UIH.GetItem(path)
anItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Sub HideAllProjects()I map these to
DTE.SuppressUI = True
Dim UIH As UIHierarchy =
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object
Dim aSoltion As UIHierarchyItem = UIH.UIHierarchyItems.Item(1)
Dim aProject As UIHierarchyItem
For Each aProject In aSoltion.UIHierarchyItems
Dim aFolder As UIHierarchyItem
For Each aFolder In aProject.UIHierarchyItems
aFolder.Collection.Expanded = False
Next
Next
DTE.SuppressUI = False
End Sub
[Ctrl]+, = Collapse all
[Ctrl]+. = Select currently active file
Very handy.
6 comments:
Excellent. Thanks for posting
Should this work under VS2005? I get an error "Public member 'Collection' on type 'DTE' not found". Any suggestions?
Nope, they changed the marco API between 2003 and 2005. The macro should be something similar though.
If you get it working please post back here with the new version.
This should help http://msdn2.microsoft.com/en-us/library/aa300825(VS.71).aspx
Thanks, Nigel
FIXED!!
To fix, change the "Microsoft Development Environment" string to "Microsoft Visual Studio".
Thanks for the script :)
I also changed this line:
Dim sln As String = DTE.Solution.Item(1).Name
To this:
Dim sln As String = DTE.Solution.FullName
sln = sln.Substring(sln.LastIndexOf("\") + 1)
sln = sln.Substring(0, sln.Length - 4)
(This picks the solution name out of the .sln filepath)
Thanks Simon.. I'll update the script.
Post a Comment