Hack 51. Create a Macro![]() 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 MacrosThe quickest way to automate simple macros is by recording your interactions with the IDE. To activate the macro recorder, go to Tools From this point until you stop the recording, your interactions with the IDE will create code in the new macro. You can pause recording to interact with the IDE without recording those actions in your macro. The recorder toolbar is shown in Figure 6-11. Figure 6-11. Macro recording toolbar![]() Recording from the macro recording toolbar or from the Tools corresponding to your actions.If you select Macro Explorer from the Tools 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. 6.9.2. Creating Macros from ScratchIf 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 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 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. Figure 6-12. The Macro 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 item![]() 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 NewModuleThe 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 ButtonMany 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 Toolbars menu. Click the New button and specify the name for your new toolbar, as shown in Figure 6-14. Figure 6-14. Enter the name for the new toolbar![]() 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 toolbar![]() 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 ShortcutCreating 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 MacrosUnlike 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 Add Existing Item and locate the file you previously exported. 6.9.6. See Also[Hack #52]. Ben Von Handorf |