1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Studio Macros

More Macro Coding Tricks

By Dan Mabbutt, About.com

Nov 25 2006

Coding VS macros can be more difficult than coding normal VB programs because the Visual Studio automation model isn't easy or well-documented. One technique that works well for discovering Visual Studio events that you can use is to display the automatically generated EnvironmentEvents Project item in the VS macros IDE. The Class Name and Method Name combo boxes then show you a list of events you can select. The illustration below shows an event macro that displays a Message Box that displays when Visual Studio saves a document.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The proof of the value of VS macros is whether you can do something actually useful. In the About Visual Basic article on Intellisense Code Snippets, I wondered why there didn't seem to be many code snippets on the web. That's certainly not the case with VS macros! There are lots of them on the web. One great example can be found at the Coding Horror web site. This macro allows you to search Google for any search string found in your program code. (Note: This particular VS macro can be found in many web pages. But most of them haven't caught up with the latest version. The one at the link above works. If the link at Coding Horror fails, you can display the source here.) I modified it to search both the Microsoft and About Visual Basic web sites for specific search strings very easily. Just replace the SearchGoogleForSelectedText subroutine with these instead.

Public Sub MSGoogleSearch()
   Dim s As String = ActiveWindowSelection().Trim() & " site:microsoft.com"
   If s.Length > 0 Then
      DTE.ItemOperations.Navigate("http://www.google.com/search?q=" & _
         Web.HttpUtility.UrlEncode(s))
   End If
End Sub
Public Sub AboutVBGoogleSearch()
   Dim s As String = ActiveWindowSelection().Trim() & " site:visualbasic.about.com"
   If s.Length > 0 Then
      DTE.ItemOperations.Navigate("http://www.google.com/search?q=" & _
         Web.HttpUtility.UrlEncode(s))
   End If
End Sub

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Visual Studio Macros

©2008 About.com, a part of The New York Times Company.

All rights reserved.