Text Copied from http://www.r2musings.com/GoogleMSDNSearchWithinVisualStudio2005.aspx
Lately, I've been deep-diving into some custom control creation in VS2005 and I'm finding that I'm spending ALOT of time on Google and MSDN2. So, I got really tired of opening the browser and surfing to Google and MSDN2 over and over and searching for things. Then I remembered someone sending me a link sometime last year where someone had created an Add-In that would perform a Google search from within Visual Studio. Well, of course, I've long since lost that link. So, I opened a browser and surfed on over to Google (where else?) and searched for "Visual Studio Add-in Google" and came across lots of links. I decided to click on the Macro for Google Search in VS.NET link as it was on Code Project and I've gotten a lot of great information from there over the years.
I have never really bothered to use a Macro in VS, but thought Id give this one a try. Right away, I got an error when I ran the macro and decided to attempt to debug through it to see if I could spot the problem. Turns out, it was failing on the following: DTE.ExecuteCommand("View.URL",strUrl). The original macro was posted to Code Project back in 2003, so I guessed that the View.URL may have been replaced in VS2005. I tried replacing the call with this: DTE:ExecuteCommand("View.WebBrowser", url) and that seemed to do the trick....sort of. It definitely fired the search in the VS2005 IDE, but it choked any time I highlighted more than one word.
I would have to say that about 99% of the time that I search for ANYTHING, it involves more than one word
.so if this couldnt be resolved, then this macro was pretty much useless to me. Anyway, with a little help from my old friend, RegEx, I now have it working exactly like I want. I have assigned a Keyboard shortcut to it and I can now highlight anything in the VS2005 IDE and press Ctrl-F1 (my custom shortcut) and I have the results for a Google Search WITHIN my IDE. Ahhh
Since that went so well, I decided to adapt it to MSDN2. (Of course, Im assuming that Ill have to update the URL in the MSDN2 macro someday soon as Microsoft moves the Library for .NET 2.0 into the standard MSDN library). However, it will be nothing more than a quick change to the URL in the macro and it should be good to go.
This is very adaptable to any search engine or site that you want. Just go there and search and see if you can figure out how they are using the QueryString for their searches and create a macro for it in VS2005.
For those not familiar with macros in VS2005, here are the steps to add a macro:
1) Go to Tools | Macros | Macros IDE
2) Once in the Macros IDE, you can create a new Module or you can just add this to the default My Macros module.
3) To add to My Macros, just right click on it in the Project Explorer and choose Add | Add New Item. (if you dont see the Project Explorer, try View | Project Explorer).
4) When the Add New Item Dialog shows up, select Code File and enter GoogleSearch and click OK.
5) Paste the Code for GoogleSearch below into this file and save it.
6) Click Debug | Build and then close the Macros IDE.
7) Once back in VS2005, click Tools | Customize and click the Keyboard button at the bottom of the Customize Dialog.
8) In the Show Commands Containing box, type macros and look for an entry that looks like this: Macros.MyMacros.GoogleSearch.GoogleSearch. (This could be different depending on where you created the macro).
9) Once you have the correct macro selected, go to the Press Shortcut Keys box and type whatever keys you want to fire the macro (I used Ctrl-F1).
10) Click the Assign button and then OK and then finally Close back on the Customize Dialog.
Here is the final code for the macro:
Imports EnvDTE
Imports System.Text.RegularExpressions
Public Module GoogleSearch
Sub GoogleSearch()
Dim url As String
Dim selectedText As TextSelection = DTE.ActiveDocument.Selection()
If Not String.IsNullOrEmpty(selectedText.Text) Then
url = String.Format("www.google.com/search?q={0}", Regex.Replace(selectedText.Text, "\s{1,}", "+"))
' for MSDN2 search, just comment out the above url line and uncomment this one
' url = String.Format("http://lab.msdn.microsoft.com/searchbeta/Default.aspx?query={0}", Regex.Replace(selectedText.Text, "\s{1,}", "+"))
DTE.ExecuteCommand("View.WebBrowser", url)
Else
MsgBox("No text selected.")
End If
End Sub
End Module
Monday, August 21, 2006
Google / MSDN Search within Visual Studio 2005#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment