andrew4582

TrackProjectItem VS Macro

May 22nd, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Public Sub TrackProjectItem()
  2.         Dim solution As Solution2 = DTE.Solution
  3.         If Not solution.IsOpen OrElse DTE.ActiveDocument Is Nothing Then Return
  4.  
  5.         solution.FindProjectItem(DTE.ActiveDocument.FullName).ExpandView()
  6.  
  7.         Dim FileName As String = DTE.ActiveDocument.FullName
  8.  
  9.         Dim SolutionExplorerPath As String
  10.         Dim items As EnvDTE.UIHierarchyItems = DTE.ToolWindows.SolutionExplorer.UIHierarchyItems
  11.         Dim item As Object = FindItem(items, FileName, SolutionExplorerPath)
  12.  
  13.         If item Is Nothing Then
  14.             MsgBox("Couldn't find the item in Solution Explorer.")
  15.             Return
  16.         End If
  17.  
  18.         DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
  19.         DTE.ActiveWindow.Object.GetItem(SolutionExplorerPath).Select(vsUISelectionType.vsUISelectionTypeSelect)
  20.     End Sub
  21.  
  22.     Public Function FindItem(ByVal Children As UIHierarchyItems, ByVal FileName As String, ByRef SolutionExplorerPath As String) As Object
  23.         For Each CurrentItem As UIHierarchyItem In Children
  24.             Dim TypeName As String = Microsoft.VisualBasic.Information.TypeName(CurrentItem.Object)
  25.             If TypeName = "ProjectItem" Then
  26.                 Dim projectitem As EnvDTE.ProjectItem = CType(CurrentItem.Object, EnvDTE.ProjectItem)
  27.                 Dim i As Integer = 1
  28.                 While i <= projectitem.FileCount
  29.                     If projectitem.FileNames(i) = FileName Then
  30.                         SolutionExplorerPath = CurrentItem.Name
  31.                         Return CurrentItem
  32.                     End If
  33.                     i = i + 1
  34.                 End While
  35.             End If
  36.  
  37.             Dim ChildItem As UIHierarchyItem = FindItem(CurrentItem.UIHierarchyItems, FileName, SolutionExplorerPath)
  38.             If Not ChildItem Is Nothing Then
  39.                 SolutionExplorerPath = CurrentItem.Name + "\" + SolutionExplorerPath
  40.                 Return ChildItem
  41.             End If
  42.         Next
  43.     End Function
Advertisement
Add Comment
Please, Sign In to add comment