Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Running Macros

After you record or manually create a macro, you can run it the following ways:

  • By a keyboard shortcut in the integrated development environment (IDE).
  • In Macro Explorer.
  • In the Find/Command box or Command window.
  • In the Macros IDE.

For security reasons, Visual Studio .NET no longer supports double-clicking a .Vsmacros or .Vsmproj file to load the macro code.

To run a macro in the IDE with a keyboard shortcut

If you have not renamed the temporary macro, you can run it by pressing CTRL+SHIFT+P or by choosing Run Temporary Macro from the Macros submenu on the Tools menu. If you have renamed it, however, you can still access it with a keyboard shortcut. Visual Studio .NET provides a way to associate (or "bind") one or more keys into a keyboard shortcut to invoke a macro. When the key sequence is pressed, the macro executes. The following procedure demonstrates how to bind a macro to a keyboard shortcut.

  1. Continuing with the example in Recording Macros, if it is not already there, restore the text document to its former state by re-typing the word "one" with a space after it, and then pressing HOME to place the insertion point at the beginning of the line.
  2. Choose Options on the Tools menu to display the Options dialog box.
  3. In the Environment folder, click Keyboard.
  4. In the Show commands containing box, type "macros." Once you do this, all commands beginning with the word "macros" appear in the commands list. (If you have not changed the temporary macro name, it should be Macros.MyMacros.RecordingModule.TemporaryMacro.)
  5. Scroll down the list to your macro.
  6. Click the Press shortcut key(s) box type a key combination, such as CTRL+SHIFT+ALT+A. This will be the keyboard shortcut that executes the macro. You can use a different key sequence if you prefer.
  7. Click Assign and then click OK. Your macro is now bound to that keyboard shortcut.
  8. Back in the text document, press CTRL+SHIFT+ALT+A to invoke the macro.

When the macro executes, the word "one" disappears from the text file. You can follow this procedure with any macro you create, assigning different keyboard shortcuts to each one.

Note   Certain keyboard shortcuts are already in use by the Visual Studio environment. When this is the case, a dialog box alerts you that the key combination you chose is one of the default keyboard mapping schemes for Visual Studio .NET and cannot be modified. It then offers to make a copy of the current scheme so that your chosen key combination can be used. You can choose to do this, but be aware that doing this might have unintended consequences. Someone, for example, who is unfamiliar with your keyboard mapping scheme might inadvertently run your macro instead of, for example, opening a file. For more information, see Customizing Shortcut Keys.

To run a macro in Macro Explorer

In Recording Macros, you recorded a small macro that, when run, deletes a word from a text document. Since running macros through Macro Explorer is the most common method, the following procedure demonstrates how to do this.

  1. If it is not already there, restore the text document to its former state by re-typing the word "one" with a space after it, and then press HOME to place the insertion point at the beginning of the line.
  2. In Macro Explorer, open the MyMacros and RecordingModule nodes, and then double-click "TemporaryMacro" to run it. Alternatively, you can press CTRL+SHIFT+P.

Notice that the word "one" is deleted.

To run a macro in the Find/Command box or Command window

You can use the Find/Command box or the Command window in Command mode to run macros.

  1. If it is not already there, restore the text document to its former state by re-typing the word "one" with a space after it, and then pressing HOME to place the insertion point at the beginning of the line.
  2. Go to the Find/Command box by pressing CTRL+/ or by clicking it at the top of the Visual Studio .NET environment's toolbar, immediately to the right of the binoculars button. To view the Command window instead, press CTRL+ALT+A. You can issue macro commands in either place.
  3. If it is not already there, type a greater than character (>) to place the Find/Command box or Command window in Command mode (versus Find or Immediate modes).
  4. After the greater than character (>), type "m" to get a drop-down list of macro-related commands. Select "Macros.MyMacros.RecordingModule.TemporaryMacro" from the list and press ENTER.

Notice that the word "one" in the text file again disappears, letting you know that the macro again ran successfully.

To run a macro in the Macros IDE

You can also run macros in the Macros IDE (not to be confused with the Macro Explorer). This is useful primarily for debugging your macros. For information about this, see Debugging Macros.

  1. If it is not already there, restore the text document to its former state by re-typing the word "one" with a space after it, and then pressing HOME to place the insertion point at the beginning of the line.
  2. Display the Macros IDE by pressing ALT+F11 or by choosing Macros IDE on the Macros submenu on the Tools menu.
  3. In the Macros IDE, right-click TemporaryMacro and choose Edit to view its code.
  4. Place your cursor in the TemporaryMacro function and press F5 or choose Start on the Debug menu.

Notice that the word "one" again disappears, letting you know that the macro again ran successfully.

You can cancel the running of a macro, such as when it is in an infinite loop, by clicking the spinning cassette icon in the Status Bar. You can also debug macros in the Macros IDE. For details about this, see Debugging Macros. For additional tips about running macros, see Macro Recording and Running Issues.

To pass parameters and switches to macros

Some macros require parameters or switches. You can pass them either programmatically or through the Find/Command box or Command window. You cannot pass parameters or switches through Macro Explorer.

  1. To pass parameters programmatically, construct the macro with the arguments in its Sub, such as:
    Sub Test(Optional ByVal FirstArg As String = "", Optional ByVal SecondArg As String = "")
       MsgBox(FirstArg & vbCr & SecondArg)
    End Sub
    
  2. When you invoke the macro, add the parameters to the call, such as:
    Test ("Test run number: ", 10)
    
  3. To pass the same parameters to the macro through the Find/Command box or Command window, list the parameters after the command invocation, such as:
    >Macros.MyMacros.Module1.Test "Test Run Number: " 10
    

See Also

Automating Repetitive Actions by Using Macros | Recording Macros | Editing Macros | Debugging Macros | Macro Recording and Running Issues | Managing Macros | Referencing COM and .NET Framework Components in Macros | Macros and the EnvironmentEvents Project Item | Macros Security and Sharing Issues | Macro Explorer Window

Show:
© 2014 Microsoft. All rights reserved.