Chapter 6, Speed Hacks
#51 Create a Macro
If you select Macro Explorer from the Tools ➝ Macros menu and open the
Recording Module, you will see a new macro called TemporaryMacro. This
is where the code for your actions during recording is created. Because the
Macro Recorder will always create the same macro routine, you must now
either copy the code to another macro module or at least rename the macro
created by the recording process, or your macro will be lost the next time
you record a macro. To change the name of the macro, right-click on the
macro in the Macro Explorer and select Rename.
Macro recording is a powerful way to quickly create fairly simple macros,
but it also has some major limitations. First, any selections in the Solution
Explorer or Class View will be specific to the file or class selected, so it will
be linked to the filename or class name. This can be overcome by editing the
recorded macro and using variables in place of the filenames.
Second, some actions, such as attaching to a process to debug, will not be
recorded. Most of these actions can be coded into macros, but you will have
to code these by hand.
Creating Macros from Scratch
If the limitations of the macro recorder make it impractical for your situa-
tion or if you are creating a complex macro, then you can create a macro
from scratch. To create macros from scratch or to edit existing macros, you
will use the Macro IDE (Tools
➝ Macros ➝ Macros IDE).
Before looking at the Macro IDE, it is a good idea to first create a new
Macro Project to store your macros in. A Macro Project is just like a regular
Visual Studio Project in that you can use it to store a number modules and
classes that include macros. To create a new macro project, click on Tools
➝ Macros ➝ New Macro Project. You will then be prompted to name your
project and choose where it will be saved.
After creating a new project, you are ready to start writing new macros using
the Macro IDE. The Macro IDE is a miniature version of Visual Studio used
exclusively for creating and editing macros. The Macro IDE is shown in
As you can see, the Macro IDE is similar to the Visual Studio IDE, including
familiar windows like the document window and the project explorer.
Macros are written using Visual Basic .NET and a special set of objects to
access the capabilities of the IDE, allowing you to create files, examine
projects, and edit documents. Because these are COM objects instead of
part of the native .NET Framework, you will find that IntelliSense support is
often somewhat inconsistent.