Udostępnij za pośrednictwem


How to: Record Macros

Recording macros is the easiest, most common way to create macros. Alternatively, you can manually create your own macros without recording. When you press the Record button, the tasks you perform, the windows and tools used, and so forth are recorded as macro code. You can also use CTRL+SHIFT+R to start or stop recording a macro. See How to: Run Macros for details about running macros.

After you record a macro, you can examine what it recorded and even change it. If the resultant macro does not do exactly what you want it to do, or if you want to add additional functionality, you can edit it after recording and customize the resultant code. For information about editing macros, see How to: Edit and Programmatically Create Macros.

Not every user action or command produces macro code. The primary areas in which Visual Studio records are the Visual Studio code editor, the Find/Replace dialog box, Solution Explorer, and window activation. For details, see Macro Recording and Running Issues.

Note

VSMacros do not currently support Windows Forms.

Recording Macros

The following procedure steps through creating an example of a simple macro.

The keyboard shortcuts used in this example assume the default Visual Studio shortcut key configuration.

To record a macro

  1. Create a new text file by choosing File on the New submenu on the File menu.

  2. Choose Text File in the General category.

  3. In the new text file, type the following:

    one two three four

  4. Press HOME to place the cursor at the beginning of the text.

  5. Press CTRL+SHIFT+R or choose Record Temporary Macro on the Macros submenu on the Tools menu to initiate the environment's macro recording mode. The Recording Toolbar appears and a small spinning cassette tape icon in the status bar indicates that you are currently recording.

  6. Press CTRL+SHIFT+RIGHT ARROW to select the word "one" and the space after it, then press DELETE.

  7. Press CTRL+SHIFT+R or click the Stop Recording button in the Recorder Toolbar to stop recording the macro. Notice that the spinning cassette icon in the status bar disappears when macro recording ceases.

    At this time, if you open Macro Explorer and expand the MyMacros node, you should see a new entry called "TemporaryMacro" under the RecordingModule node. This is the default name given to newly-recorded macros.

Warning

The macro has not been saved yet. If you want to save it, click "Save Temporary Macro" on the Macros submenu on the Tools menu. Alternatively, you can right-click TemporaryMacro and choose Rename. This allows you to rename the macro and prevent it from being overwritten by the next new macro.

Because the majority of macros are used only for a short time and then discarded, TemporaryMacro is overwritten each time a macro is recorded. If you want to reuse the macro later, you can rename TemporaryMacro to prevent it from being overwritten by the next recorded macro.

Note

If you accidentally start recording a macro, you can prevent deletion of your previous temporary macro by canceling the macro recording. If you do this, Visual Studio reverts back to your previous macro code in TemporaryMacro.

While recording a macro, you can pause the recording and then resume it later. Typically, this is used for making preparations in the environment that will be required by the macro, but that you do not want to record.

To cancel recording of a macro, either click the Cancel Recording button on the Recording toolbar, or click the spinning cassette icon in the Status Bar.

Renaming the Temporary Macro and Specifying the Recording Project

Newly-recorded macros are named "TemporaryMacro" by default. As a result, if you record a macro, then immediately record another one, the first one is lost unless you previously saved or renamed it.

To rename (and save) a newly-recorded macro

  1. Right-click the macro's name.

  2. Choose Rename, and then type a new name, or use the Save Temporary Macro command on the Macros submenu on the Tools menu. You can also rename the temporary macro using the Macros IDE as well.

All recorded macros are placed by default under RecordingModule in the macro project that is specified as the recording project. The recording project name displays in bold text to indicate that it is the current recording project. The default recording project module is MyMacros. If you prefer, you can move your macros to another module.

Warning

You should rename the Temporary Macro before moving it. Otherwise, it can cause problems when recording new macros.

To create a new macro module

  1. Right-click MyMacros.

  2. Choose New Module.

To specify another macro project as the recording module

  1. Right-click the project.

  2. Choose Set as Recording Project.

See Also

Tasks

How to: Manage Macros

How to: Reference COM and .NET Framework Components in Macros

How to: Handle Environment Events in Macros

Reference

Macro Explorer Window

Concepts

Automating Repetitive Actions by Using Macros

Debugging Macros

Macros Security and Sharing Issues