Google, Live, MSDN Search within Visual Studio 2008

Since Matt Ranlett has now harassed me several times for removing the Google/MSDN macro from my blog, I decided to repost it.  I have added Live Search to the mix this time and I have tested all of the searches on Visual Studio 2008 Beta 2.

The background on this was that I was spending a lot of time on Google and MSDN (aren’t we all?) and was going back and forth from Visual Studio to the browser, so I decided to try and integrate my searches into Visual Studio.  The resultant experience is that you can highlight any text in Visual Studio and press your assigned keyboard shortcut and receive search results from your favorite search engines WITHIN the Visual Studio IDE.  It really comes in handy! 

In the code below, I have provided the URLs for searching against Google, Live, and MSDN.  It should be pretty obvious when you look at the macro what you would need to do to configure any other search engines.  Be sure to uncomment one (and only one) of the

url lines in the code when you setup each macro. 
 

For those not familiar with macros, here are the steps to add a macro in Visual Studio:

 

1)      Goto 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 don’t 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. Make
sure that you uncomment one of the url lines in the code.

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 Alt-G for Google, Alt-M for MSDN, Alt-L for Live).

10)  Click the Assign button and then OK and then finally Close back on the Customize Dialog.

11) Highlight any text in the IDE and fire off your shortcut key to test.

12) Repeat for each search macro you want to setup. 

Here is the final code for the macro:

 

  1. Imports EnvDTE
  2. Imports System.Text.RegularExpressions
  3. Public Module GoogleSearch
  4.     Sub GoogleSearch()
  5.         Dim url As String
  6.         Dim selectedText As TextSelection = DTE.ActiveDocument.Selection()
  7.         If Not String.IsNullOrEmpty(selectedText.Text) Then
  8.             ' uncomment below for Google
  9.             ' url = String.Format("www.google.com/search?q={0}",
  10.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  11.             ' uncomment below for MSDN search
  12.             ' url = String.Format("http://search.msdn.microsoft.com/search/Default.aspx?brand=msdn&query={0}",
  13.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  14.             ' uncomment below for Live search
  15.             ' url = String.Format("http://search.live.com/results.aspx?q={0}",
  16.             '        Regex.Replace(selectedText.Text, "\s{1,}", "+"))
  17.             DTE.ExecuteCommand("View.WebBrowser", url)
  18.         Else
  19.             MsgBox("No text selected.")
  20.         End If
  21.     End Sub
  22. End Module

This entry was posted in Macros, Visual Studio 2005, Visual Studio 2008. Bookmark the permalink.

Comments are closed.