VS.NET Google Macro

I wrote the following macro a while back as a response to a question about searching Google from within VC6 on the ATL List. Simon Fell responded and pointed out that he had written an add-in for VC6 to support this. I felt like learning the VS.NET object model at the time so I took a few minutes and drafted this up. It's really simple, but at least it's a start. I suppose with the new Google API I could get a lot fancier and even have my own search results window.

To use it, simply open the macro editor, paste it in, then go to Tools -> Options -> Environment -> Keyboard and bind it to whatever key claw you'd like. You can also execute the macro via the command window if you prefer.

Enjoy!

Macro:

Imports EnvDTE

Imports System

Imports System.Windows.Forms

 

Public Sub SearchGoogleForSelectedText()

  Dim ActiveDocument As Object = DTE.ActiveDocument

    If Not ActiveDocument Is Nothing Then

      Dim Selection As TextSelection = CType(ActiveDocument.Selection, TextSelection)

      Dim SelectedText As String = Selection.Text

      

      If SelectedText.Length > 0 Then

        Dim GoogleUriBuilder As UriBuilder = New UriBuilder()

 
        GoogleUriBuilder.Scheme = Uri.UriSchemeHttp

        GoogleUriBuilder.Host = "www.google.com"

        GoogleUriBuilder.Path = "/search"

        GoogleUriBuilder.Query = "q=" & SelectedText

        DTE.ItemOperations.Navigate(GoogleUriBuilder.Uri.ToString())

      Else

        MessageBox.Show("No text selected.", "Google Search", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

      End If

    Else

      MessageBox.Show("No active document.", "Google Search", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

    End If

End Sub