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
Figure 6-12.
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.
Create a Macro #51
Chapter 6, Speed Hacks
The base object for almost all macro commands is DTE, which stands for
Development Tools Extensibility
[Hack #86]. It represents your Visual Studio
IDE session and has a large number of object collections and properties. For
macros in which you want to edit the text of the current file, the
ActiveDocument property will be the one you are primarily concerned with.
For operations such as Find and Replace, there is a
Find property in the DTE.
The documentation for the
DTE object is fairly complete, but often using the
macro recorder will allow you to figure out which methods or properties are
involved in a given operation.
Macros are organized into modules for convenience, so before you start cre-
ating a macro’s functions and subs, it is best to create a new module to put
them in. To add a new module, right-click on your project in the Project
Explorer and choose Add New Item. The Add New Item dialog is shown in
Figure 6-13.
From this dialog, you can name your module, and when you click Open, the
module will be added to your project and opened in the document window.
From this point, you can start writing your macro.
For a short sample macro, this code will add the specified text to the begin-
ning of the active document:
Public Module NewModule
Public Sub SampleMacro( )
DTE.ActiveDocument.Selection.StartOfDocument( )
Figure 6-12. The Macro IDE

Get Visual Studio Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.