Build a Quick Visual Studio.NET Google Search Macro
By Peter A. Bromberg, Ph.D.

Peter Bromberg

The builders of Visual Studio.NET put a lot of time and thought into making a world-class IDE for developers. And one of the coolest things they did was to put in a robust Extensibility Framework that allows developers to create add-ins and macros to automate useful tasks and get productivity gains.

Do you ever find yourself working in Visual Studio and firing up your browser to do a Google Search to find a sample of some code syntax you are working on? Well, with this handy macro and the power of the IDE, all you will ever need to do is highlight the phrase you want to search on, hit a shortcut key combination of your choosing, and all the Google search results will appear right in your Edit Window area; you'll never have to leave the IDE to do it.



Trust me, I'm not the first guy to think of putting in a Google search macro - you can search Google yourself and I am sure you'll find a number of examples; probably some are more sophisticated than this one. But as a big proponent of the "Less is More" philosophy, I think this code is "just enough" to get the job done right!

First, fire up Visual Studio.NET, and choose Tools->Macros->MacroExplorer (alt -T -M -M for you shortcute dewds). You should see the Macro Explorer window, and it most likely will show "My Macros" and "Samples" nodes.

Next, right click on the topmost Macros node and choose "New Macro Project", and give it a nice name like "GoogleSearch". Double-click on your new node and it should open up the edit window on the module. Paste in the following code:

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

Now, you can Build and Save (Ctrl-S) and close the editor.

Now, go into Tools->Options->Environment->Keyboard from the top menu. In the "Show commands Containing" wndow you should be able to type "Google" and your project will show up.

Now set focus in the "Press Shortcut Key" window and choose a shortcut key combination, like alt SPACE or whatever you want. Then hit the "Assign" button. Almost all of these will prompt you that the selected scheme is "One of the default keyboard mappings", which is kind of non-intuitive, but go ahead and Choose "Yes" to make a copy. This is what actually assigns your keyboard shortcut to your Macro.

At this point, you are 100% DONE! Load up a project, highlight some text in the editor window, and hit alt-SPACE or whatever your shortcut was, and presto (the real thing will look a lot better):

 

 

Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform.