Introduction to Macros in Visual Studio 2008


In this article, we will look into Macros in VS 2008. Macros allow us to automate our repetitive tasks. Macros records all the tasks, we are doing in VS and plays it back. This way, we can record our repetitive tasks in Macros and run as need arises on any machine. Macro is a series of commands and instructions that are grouped together as a single entity to perform a task automatically. VS provide a separate Macros Editor for creating/editing/running macros. Macro IDE is not part of VS built-in IDE.

Let's start the things. Open your VS 2008 and create a sample web application with name as MacrosTest. Now, go to Tools -> Macros -> Record TemporaryMacro [Ctrl+Shift+R]. I recorded few activities done in my default.aspx.cs like adding a textbox, button. Than, click on "Stop Recording" as shown below:



Now, run the macro by going to Tools -> Macros -> Run TemporaryMacro [Ctrl+Shift+P]. Since, this macro is not saved; it will be called as Temporary macro. Save that macro by going to Tools -> Macros -> Save TemporaryMacro as Macro1. There are lots of built-in macros, which can be explored using Macro Explorer as shown below:



Now, we will assign a keyboard shortcut by going to Tools -> Customize -> Keyboard -> Keyboard Tab -> Search command -> Give Shortcut Key -> Ctrl + Shift + '. From now onwards, we can use this shortcut to run our macro.

We will move into debugging macros. When errors occur in your macros, we can debug the code in Macros IDE by using breakpoints. We can catch runtime errors by running the macro in debugging mode by starting macro by pressing F5. By default, debugging mode is enabled if a breakpoint exists on a macro in Macro IDE.

Now, we will look into Macro Explorer. This explorer allows us to track and manage macros. We can perform following activities using this Explorer:

  • Create new macro projects & modules.
  • Load/Unload existing macros.
  • Delete/Rename macros and modules.
  • Edit macros and modules.
  • Run existing macros.

By default, all macros are stored in \VSMacros80 in the directory specified by the VS location text box in Projects & Solutions node of Options dialog box.

We can export macro modules by using Macros IDE. Just, select a module and right click, select Export. This will export the module as a VB file.

Now, we will move into handling Environment events in macros. Every new macro project includes a default module named as EnvironmentEvents, which can be accessed only in Macros IDE. This module contains a number of pre-defined event procedures for using in our macros.

I will list few of the important events:

  • DTEEvents: Provides events relating to the state of the environment.
  • WindowEvents: Provides events for changes made to Windows in the environment.
  • TaskListEvents: Provides events for changes made to the Task List.
  • BuildEvents: Provides a list of events for solution builds.
  • FindEvents: Provides events for Find-in-Files operations etc.

Let's see an example using FindEvents. Go to your Macros IDE and open Samples macro project. Than select EnvironmentEvents module and select FindEvents class and FindDone event as shown below:



Now add the below code to FindDone:

Private Sub FindEvents_FindDone(ByVal Result As EnvDTE.vsFindResult, ByVal Cancelled As Boolean) Handles FindEvents.FindDone
        If Result = vsFindResult.vsFindResultFound Then
            MsgBox("Items Found")
        ElseIf Result = vsFindResult.vsFindResultNotFound Then
            MsgBox("Items Not Found")
        End If
 End Sub


Now, run the macro by pressing F5. When we do a search in any VS instance, it will show a message box based on search results. In this way, we can add our custom code to other VS Events.

We can distribute macros for using on other machines in the form of source code only. Since, we cannot compile it .Visual Studio macros are free from viruses, since no macro code can run automatically on opening the project. We need to run explicitly macros. We can even see the macro code, before running to ensure that it is safe and secure.

I am ending up the things here. I hope this article will be helpful for all.


Similar Articles