How to: Edit and Programmatically Create Macros
Once you record a macro, you might want to edit its code to add, change, or delete functionality, or you might want to manually create your own macro rather than record one. The Macros IDE is the tool that allows you to accomplish these tasks.
Even though you can manually create macros from scratch in the Macros IDE, editing a recorded macro can save steps. For example, if you close a specific window, such as Solution Explorer, at the end of a recording, you could easily modify the code to close the currently active window rather than Solution Explorer.
In How to: Run Macros, you recorded a small macro. The following procedure demonstrates how to view the code that the environment generated and edit it.
Edit a Macro
To edit a macro
Right-click the newly-created macro and choose Edit.
Notice that your macro appears in the Macros IDE. The automatically generated code was:
Sub TemporaryMacro() DTE.ActiveDocument.Selection.WordRight(True) DTE.ActiveDocument.Selection.Delete() End Sub
To cause two words to be selected to the right rather than only one, add an extra argument (,2) to the end of the first line of code:
DTE.ActiveDocument.Selection.WordRight(True, 2)
Choose Save to save your macro.
Return to the text file you created in How to: Run Macros.
Add the word "one" to the beginning of the line, if it is missing.
When you run the macro (CTRL+SHIFT+P), the words "one" and "two" are deleted rather than just the word "one."
Note
Normally, you run your macros in the main environment in Macro Explorer or in the Command window or Find/Command box. If you run your macros in the Macros IDE, the results occur in the main Visual Studio environment, so you must switch to that window to see the results of the macro.
Create a New Macro
In some cases, you might want to manually create a macro rather than record one.
To manually create a new macro
In Macro Explorer, right-click Module1 and choose New Macro.
This displays the following blank procedure in the Macros IDE:
Sub Macro1() End Sub
Replace the generated code by the following code:
Sub MyTest() MsgBox ("Hello world.") End Sub
Press CTRL+S or choose Save to save your macro.
You can run the macro as you would a recorded macro. See How to: Run Macros.
See Also
Tasks
How to: Reference COM and .NET Framework Components in Macros
How to: Handle Environment Events in Macros
Concepts
Automating Repetitive Actions by Using Macros