Export (0) Print
Expand All

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.

To edit a macro

  1. 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
    
  2. 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)
    
  3. Choose Save to save your macro.

  4. Return to the text file you created in How to: Run Macros.

  5. 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."

    NoteNote

    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.

In some cases, you might want to manually create a macro rather than record one.

To manually create a new macro

  1. In Macro Explorer, right-click Module1 and choose New Macro.

    This displays the following blank procedure in the Macros IDE:

    Sub Macro1()
    
        End Sub
    
  2. Replace the generated code by the following code:

    Sub MyTest()
      MsgBox ("Hello world.")
    End Sub
    
  3. 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.

Community Additions

ADD
Show:
© 2015 Microsoft