Documenting has never been so easy

Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

Recently, I was asked an interesting question. How to get search results of Find In Files operation in VS? You can get these results quite easily from your macro or add-in. The trick is to read the contents of Find Results 1 or Find Results 2 window, whichever was used to show the results. So, the first thing is to determine which of them was used. We do it right after manually performed search from menu or our call of DTE.ExecuteCommand("Edit.FindinFiles"). The DTE.Find.ResultsLocation property will tell us this. Then we find the window and get its content.

The whole code which gets results of the last Find In Files operation is:

Dim resultsWin As Window = DTE.Windows.Item(DTE.Find.ResultsLocation)
Dim sel As TextSelection = resultsWin.Selection
Dim searchResults As String = sel.Text

You can use this approach to read the contents of other VS windows too. Just investigate EnvDTE.Constants as parameters for DTE.Windows.Item function. But remember that not all windows objects have set their Selection property directly. For example the Output window may have several panes. To get the text from active pane of Output window use this code:

Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim oWin As OutputWindow = win.Object
Dim sel As TextSelection = oWin.ActivePane.TextDocument.Selection
Dim outputText As String = sel.Text

And finally, you want to read the text quietly, without modifying current cursor or selection. Our methods select all text. So it would be good to restore original selection. To do so, add this code before sel.SelectAll():

'remember selection
Dim oldAnchor, oldActive As EnvDTE.TextPoint
oldAnchor = sel.AnchorPoint.CreateEditPoint
oldActive = sel.ActivePoint.CreateEditPoint

And after reading the text by sel.Text add this code:

'restore selection
sel.MoveToAbsoluteOffset(oldAnchor.AbsoluteCharOffset) 'set active point
sel.SwapAnchor() 'set anchor to active point
sel.MoveToAbsoluteOffset(oldActive.AbsoluteCharOffset, True)



Start generating your .NET documentation now!


Free, fully functional trial

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Set of techniques which have for object the commercial strategy and in particular the market study.