Quick and Easy Google (or Bing) Web Search in Visual Studio

posted on 03 Mar 2010 | Visual Studio

Here is a quick and easy way to add a Google (or Bing) Web Search link to the context menu in Visual Studio:

  • Open the Visual Studio macros IDE by navigating to Tools –> Macros –> Macros IDE (or pressing Alt+F11).

- Right click on “MyMacros” and select Add New –> Add New Item.

- Select the Module template, name the module “Search”, and click Add.

- Paste the following code directly before the “End Module” line:

Sub Search()
    Dim strUrl As String
    Dim selection As TextSelection = DTE.ActiveDocument.Selection()
    If selection.Text <> "" Then
        strUrl = "www.google.com/search?q=" + selection.Text
        ' strUrl = "www.bing.com/search?q=" + selection.Text
        DTE.ExecuteCommand("View.URL", strUrl)
    Else
        MsgBox("Select Text first to Search")
    End If
End Sub

- (Optional) If you prefer to use Bing for your search, uncomment the Bing line and comment out the Google line.

- Your macro editor window should now look something like this:

Screenshot of Macro Code

- Save and close the Macro Editor window and IDE.

- Right click on the Visual Studio toolbar and select Customize.

- Under the Toolbars tab, check the Context Menus option and the switch to the Commands tab.

- Select the Macros Category, and then select the “MyMacros.Search.Search” macro.

- Drag the selected macro onto “Editor Context Menus” -> “Code Window” and then drop it where you want it in the context menu (I place mine below the Paste command).

- Right click on the new context menu item and change it’s name to “&Web Search”.

- Click close on the Customize window.

- You are now done! If everything has gone according to plan, when viewing a source code file you should now see a new context menu item that will do a web search on any selected text when you click it:

Screenshot of Context Menu

Screenshot of Search Result in VS

comments powered by Disqus