Preamble
Using F1 to access the MSDN Library is all well and good, but there are times when you really want to use an internet search engine, so here is one way to do it.
Visual Studio Macro
The first step is to write the macro code in the IDE. To do this, open the Macro Editor:
Visual Studio macros are written in VB.NET, so you need to create a new module.
Imports EnvDTE
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Globalization
Imports System.Web
Public Module Search
Public Sub Google()
NavigateTo("http://www.google.com/search?q={0}")
End Sub
Private Sub NavigateTo(ByVal url As String)
Dim selection As String = DTE.ActiveDocument.Selection.Text
If selection.Length > 0 Then
DTE.ItemOperations.Navigate(String.Format(CultureInfo.InvariantCulture, url, HttpUtility.UrlPathEncode(selection)), vsNavigateOptions.vsNavigateOptionsNewWindow)
Else
MessageBox.Show("No search text selected.")
End If
End Sub
End Module
Update: Tweaked the code to encode the selection text (thanks rippleyong)
Create the toolbar
The next step is to create a new toolbar. Close the Macro Editor and bring up the Customize dialog (either from the toolbar context menu or Tools >> Customize). Create a new toolbar (I called mine "Search") on the Toolbars tab. To add new buttons to the newly created Search toolbar, select the Commands tab then select the Macros Category and scroll to the newly created command:
Simple drag this onto the Search toolbar and the toolbar button is created:
The new button can now be edited. Note that you must be in customize mode to do this. The first step is to change the name from the Macro name as this will become the tooltip text. I changed mine to "Google":
The next step is to set up the icon. The easy way to do this is to simply select from one of the standard icons:
Personally, I prefer to use custom icons. There is a very simple hack to do this for an internet search engine. Just navigate to the site favicon (for Google it is http://www.google.com/favicon.ico). Then just copy the icon:
To apply to copied icon, just paste it:
The toolbar now looks like this:
The last step is to select Default Style to display the icon without text:
To use the toolbar, simply select the text you want to search on and click the button representing the desired search engine.
Extending the toolbar
It's very easy to add any other engines that you want.
I added search buttons for Live Search and for my own del.icio.us bookmarks. Here is the additional macro code:
Public Sub Delicious()
NavigateTo("http://del.icio.us/alan.dean/{0}")
End Sub
Public Sub Live()
NavigateTo("http://search.live.com/results.aspx?q={0}")
End Sub
Sometimes the icon does not paste properly. As an example, the Live Search favicon loses transparency. Luckily, Visual Studio provides a way of editing the pasted icon:
The following dialog is displayed to edit the icon:
After adding these two additional buttons and docking the toolbar, it looks like this:








