How to create, use, and manage macros in Visual Studio .NET or in Visual Studio 2005 (317347)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Studio .NET (2003), Professional Edition
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
This article was previously published under Q317347 SUMMARY This article demonstrates how to create a new macro project
in Visual Basic .NET or in Visual Basic 2005, and then record a simple macro that inserts a TODO token
into the code and subsequently opens the Task List window to view the TODO
task. It also shows you how to map the macro to a keyboard
shortcut.
back to the topRequirements You need the following hardware and software to perform the
procedures in this article:
- Microsoft Windows Server 2003, Microsoft Windows 2000 Professional (or Microsoft Windows
2000 Server) or Microsoft Windows XP Professional with the .NET Framework
installed.
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
back to the topCreate, Use, and Manage Macros The following procedure illustrates how to create a new macro
project and then record a simple macro that inserts a TODO token into the code
and subsequently opens the Task List window to view the TODO task. The macro is
also mapped to a keyboard shortcut. To create, use, and manage
macros, follow these steps:
- Click Start, point to Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then
click Microsoft Visual Basic .NET or Microsoft Visual Basic 2005.
- Click New Project.
- In the Project Types list, click Visual Basic Projects.
Note In Visual Studio 2005, click Visual Basic under Project Types. - In the Templates list, click Console Application.
It does not matter which type of application you
choose to demonstrate the creation and use of the following sample macro. A
console application is appropriate here because it is simple and
lightweight. - Click OK.
Module1.vb should now be open in the Code Editor
window. - To illustrate how macros are managed, click Tools, point to Macros, and then click Macro Explorer to open Macro Explorer (or press ALT+F8).
This is the
first step to create a new macro with the Visual Studio .NET or Visual Studio 2005 macro recording
feature.
The MyMacros project should be visible. It is installed by
default in the VSMacros folder beneath the Visual Studio projects location, as
set in the Projects and Solutions folder (click Tools, point to Options, and then click Environment).
NOTE: To manage (and organize) macros, create new macro
projects. - In the Macro Explorer, right-click Macros, and then click New Macro Project.
- In the New Macro Project dialog box, type KeyboardUtil in the Name box, and then click Open.
Note In Visual Studio 2005, click Add instead of Open. - In Macro Explorer, right-click KeyboardUtil, and then set this as Recording Project.
Newly recorded macros will now be placed in this new
macro project. - The macro that you will create inserts the TODO token into
your code and then opens the Task List window with all tasks in view. This
token is automatically parsed by the integrated development environment (IDE)
to aid developers in marking up their code.
NOTE: A full list of tokens and Task List options are available when you click Tools, point to Options, point to to Environment, and then click Task List. - Press CTRL+SHIFT+R to start recording the macro.
A small Recorder toolbar should appear. All interactions with the keyboard and
Visual Studio .NET interface are now recorded. - In the Code Editor window, type
TODO: inside the Sub Main() frame in the
code.
- Click View, point to Show Tasks, and then click All.
- Press CTRL+SHIFT+R to stop recording.
- In Macro Explorer, notice that a new file called
RecordingModule is now displayed, with TemporaryMacro underneath it.
Right-click RecordingModule, and then click Edit to open the Macros IDE and view the code that was generated when
you recorded your actions in Visual Studio .NET or in Visual Studio 2005.
Here you can edit
the code directly, or write macros from scratch. NOTE: When you view this code, check it against the code included in
the "Complete Code Listing" section of this article. The macro-recording
process records all clicks and key presses that you make. Therefore, if you
make an extra mouse click, this will be recorded. The resulting extra line will
most likely be as follows: DTE.Windows.Item("Module1.vb").Activate() This line moves the Active Window to Module1.vb. If you want this macro
to work in whatever window is currently active, remove this line. - Close the macro IDE.
- The macro is not saved until you rename it. In Macro
Explorer, right-click TemporaryMacro, and then click Rename. Rename the macro as
TODOWriter.
- Although you can run the macro directly from Macro
Explorer, it is most convenient to associate it with a keyboard shortcut. To do
this, click Tools, point to Options, point to Environment, and then click Keyboard.
- In the Show commands containing box, type
the following: todo.
The macro that you just
created should appear in the list immediately underneath. - Place the mouse pointer in the Press shortcut
keys box, and then press CTRL+SHIFT+RIGHT ARROW.
The
shortcut currently used by this field should alert you to the fact that this
keyboard combination is already in use for the Edit.SizeControlRight command. - Press BACKSPACE to delete the key combination. Press
CTRL+SHIFT+BACKSPACE. This combination is not in use; therefore, click Assign.
- When prompted, click Yes, and then type any name for the new keyboard scheme, which is a
copy of the default scheme with the new macro mapping. Click OK twice.
back to the topComplete Code Listing (RecordingModule)
Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics
Public Module RecordingModule
Sub TODOWriter()
DTE.ActiveDocument.Selection.Text = "' TODO:"
DTE.ActiveDocument.Selection.NewLine()
DTE.ExecuteCommand("View.All")
End Sub
End Module
back to the topVerification- Reset the IDE environment. To do this, click View, point to Show Tasks, and then click Build Errors.
This ensures that the TODO tasks are not in view.
Remove any code that you typed within the Sub Main() frame. - With your mouse pointer positioned inside Sub Main(), press
CTRL+SHIFT+BACKSPACE.
A TODO token should be inserted, a new line
created, and the TODO task should now be visible in the Task List window. To
see how the TODO task works, type some task I need to perform
later after TODO:, and then press DOWN ARROW to move your pointer to the next line
and cause the task to appear. NOTE: When the macro is finished running, the Task List is the active
window. If you press ESC, you return control to the Code Editor, where the TODO
token was inserted by the macro. - In Macro Explorer, double-click TODOWriter to run the macro. (Before you rename a newly recorded macro, you
can also run it by pressing CTRL+SHIFT+P.)
back to the topTroubleshooting As noted in the .NET Framework SDK documentation:
- You cannot run a macro if its parent project cannot be
built, such as when another macro in the project contains errors.
- When you run a macro from Macro Explorer, the environment
considers the last window that was opened immediately before you open Macro
Explorer to be the last activated window, and therefore the macro runs as if
that window has focus. This prevents problems with the Macro Explorer window
inadvertently gaining the focus in your macro's operation.
back to the topREFERENCES- For more information about how to use macros in Visual
Studio .NET, browse to the following MSDN Web site:
- For information about how to use macros to automate
repetitive actions, see the Microsoft Web Help file at the following Microsoft
Web site:
- For information about how to run macros, see the Microsoft
Web Help file at the following Microsoft Web site:
- For information about how to manage macros, see the
Microsoft Web Help file at the following Microsoft Web site:
back to the top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbHOWTOmaster kbinfo KB317347 kbAudDeveloper |
---|
|