Hack 51. Create a MacroLearn how to create and run macros that automate mundane development activities. Creating a macro to help you with your daily tasks is an excellent way to automate and improve repetitive operations. There are several ways to create a macro: either recording your actions in the IDE or creating a macro from scratch. After you have your macro built and tested, you can also create a toolbar button that launches your macro or create a keyboard shortcut. 6.9.1. Recording Macros
The
quickest way to automate simple macros
is by recording your interactions with the IDE.
To activate the macro recorder, go to Tools Figure 6-11. Macro recording toolbarAfter you have performed the actions you want to record, select Stop
Recording from the macro recording toolbar or from the Tools
If you select Macro Explorer from the Tools 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. 6.9.2. Creating Macros from Scratch
If the limitations of the macro recorder
make it impractical for your situation 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 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 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. Figure 6-12. The Macro IDEAs 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. 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 creating 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. Figure 6-13. Adding a new itemFrom 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 beginning of the active document: Public Module NewModule Public Sub SampleMacro( ) DTE.ActiveDocument.Selection.StartOfDocument( ) DTE.ActiveDocument.Selection.NewLine( ) DTE.ActiveDocument.Selection.Text = "This is a test" DTE.ActiveDocument.Selection.NewLine( ) End Sub End Module The first line of the Sub called StartOfDocument()will move the cursor to the top of the document. The second line will create a new line. The third line of this macro will insert the string "This is a test", and the last line will create another new line. After you have written your macro in the Macro IDE, you can save the file and then close the IDE. Your macro will now appear in the Macro Explorer and can be run from the IDE. 6.9.3. Adding a Toolbar Button
Many macros will be useful in your daily life, so having to locate them through the Macro Explorer each time can be inconvenient. Thankfully, macros can be added to a toolbar. To avoid cluttering existing toolbars, it is a good idea to create a
new toolbar. First, select Customize from the View Figure 6-14. Enter the name for the new toolbarAfter creating the toolbar, a ghost of it will appear, as shown in the bottom right of Figure 6-15. You can now select the Commands tab where you can add your macro to the new toolbar. Figure 6-15. Adding a macro to a toolbarIn order to select the macro, you must first select the Macros category in the Categories listbox. After scrolling past the sample macros (if they are installed), the macros from your own projects will appear. Select the macro you want to create a button for and drag it into the desired position on the toolbar where it will appear as a button. After creating your toolbar, you can simply close the customization window and dock the toolbar wherever it is convenient for you in your IDE. For more information on customizing this toolbar, including how to create an image for your toolbar item, please refer to [Hack #25]. 6.9.4. Creating a Keyboard Shortcut
Creating a keyboard shortcut is an even easier way to make your macro accessible during your normal document editing. A macro will simply appear as another command in the list available when assigning a keyboard shortcut as described in [Hack #24]. 6.9.5. Sharing Macros
Unlike normal Visual Studio Projects, Macro Projects are contained in a single file instead of a separate file for each module. If you have a module you wish to share among multiple users, you must first export it. Here is the process for exporting a macro: Open the module you wish to export. Click File Choose where to save the file to. When you wish to import the module on another machine, simply select Add Existing Item and locate the file you previously exported. 6.9.6. See Also
Ben Von Handorf
|