Google search VS.NET macro

Here's a handy little Visual Studio .NET macro which searches for the currently highlighted term in Google. The search is launched as a new tab within the IDE when you press


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html

I’d love a way for it to find the word the cursor is currently on, withOUT needing to select it. (That’s how VS.net’s integration with MSDN Library works.)

I don’t care for this behavior personally, but I guess it’s better than doing nothing, which is what happens when you hit ALT+F1 with nothing selected. I added it to the code in the entry…

Next problem is that it doesn’t find any selected text

I can’t duplicate this at all. The IDE always finds my selected text using DTE.ActiveWindow.Selection.

How are you doing this?

Awesome! I added a CTRL+G mapped macro that did the same stuff but used http://www.google.com/q=site:msdn.microsoft.com+ as the query URL so that you can search MSDN through Google’s interface.

Love it. It’s the first macro that I can use on a regular basis so I’ll remember the short cut. Have you tried it in the Html view of an aspx page?

That is really nice. Thanks a lot I will be putting that to good use.

Neat. I’ll probably tweak it so it launches an external IE window though.

Great stuff!

I changed the URL to hit MSDN. Here is the line of code with the URL:

DTE.ItemOperations.Navigate("a href="http://search.microsoft.com/search/results.aspx?view=msdnqu=“http://search.microsoft.com/search/results.aspx?view=msdnqu=”/a HttpUtility.UrlEncode(s))

There are very few macros that are actually usefull. This one is definitly tops!

If someone gets the tweak to open in a new browser, can you post the code?

If someone gets the tweak to open in a new browser, can you post the code?

If you want IE to open in a seperate window instead of an IDE tab, just change…

DTE.ItemOperations.Navigate

to…

Diagnostics.Process.Start

As it is, using the macro in VS.net 2003 gives me this error:
“Name ‘HttpUtility’ is not declared.” I had to import the System.Web namespace too, since HttpUtility is unqualified:
Imports System.Web

So I fixed that error. Next problem is that it doesn’t find any selected text.

In debugging, this is true:
DTE.ActiveWindow.Selection Is Nothing.
But this (change) is false:
DTE.ActiveDocument.Selection Is Nothing

So changing ActiveWindowSelection thus fixes it for me:
Return SelectionText(DTE.ActiveDOCUMENT.Selection)

BTW, I also modified it to use Google’s Microsoft-targeted search URL:
a href="http://www.google.com/microsoft?q="http://www.google.com/microsoft?q=/a

I’d love a way for it to find the word the cursor is currently on, withOUT needing to select it. (That’s how VS.net’s integration with MSDN Library works.)

For this sort of behavior elsewhere than MSVC.net I’d like to recommend ClipX (a tiny clipboard history manager) at a href="http://bluemars.org/clipx/"http://bluemars.org/clipx//a which has the nifty feature of opening a browser with a Google search for the current clipboard contents with Control + Shift + G (or any other key you wish to configure for it.) Not quite as streamlined in that you have to first copy the text you wish to search but still quite useful.

Here’s how to open the P/Invoke website for native win32 commands:

DTE.ItemOperations.Navigate("a href="http://www.pinvoke.net/search.aspx?search=“http://www.pinvoke.net/search.aspx?search=”/a HttpUtility.UrlEncode(s) “namespace=[All]”)

Does the change to Diagnostics.Process.Start work for anyone? For me it causes this error: “The requested lookup key was not found in any active activation context.”

if you change the -

DTE.ItemOperations.Navigate(“http://www.google.com/search?q=” HttpUtility.UrlEncode(s))

to

DTE.ItemOperations.Navigate("http://www.google.com/search?q=.net " HttpUtility.UrlEncode(s))

then you get searches based on .net only

Jon – regardless of the way the call is qualified, you’ll still have to manually add a reference (via the right click Add Reference menu) to the IDE module.

If that wasn’t required, I’d totally agree with you.

And I also agree that simply “HttpUtility” alone is no good because it gives the developer no hint as to what reference is required. But I think “Web.HttpUtility” should be enough to figure it out. I mean, c’mon. :wink:

Ever useful insight as always Jeff;) This one rocks!

I posted a article that explaind how to use googled search in Visual Studio 2003 about 2 years ago, and, of course, your version is better than mine. :slight_smile:

http://www.codeproject.com/macro/googlemacro.asp

How can you get green text on a black background in your output window…?

It’s only possible in Visual Studio 2005, via Tools, Options, Environment, Fonts and Colors, [All Text Tool Windows]

How can you get green text on a black background in your output window…? Great macro, cheers!