The Visual Studio automation model is open to your macro statements through the DTE object. Note that the code template provided in the recorded macro includes these statements to reference the object models for three versions of Visual Studio:
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
The way to turn on line numbers is to change a property in the Visual Basic editor. In the VS macro IDE, replace ...
DTE.ExecuteCommand("Tools.Options")
... with ...
DTE.Properties("TextEditor", "Basic").Item("ShowLineNumbers").Value = True
Many VS macro statements will reference the DTE object. You can address a lot of the Visual Studio environment using these properties. Check out the Creating and Controlling Environment Windows page at MSDN for details.
To test our new VS macro, simply double click the icon for TemporaryMacro in Visual Studio (not in the VS macro IDE). Line numbers will be turned on.
The final task is to clean up our VS macro for use, copy it from the RecordingModule where the VS Macro Recorder created it to a new module. Macros can be placed in any directory, but they have to be "loaded" before Visual Studio can use them. Normally, Visual Studio will create and load an initial Module1 in your MyMacros vsmacros file. (Keep in mind that these are "virtual files" and not real ones. If you look in the MyMacros folder using Windows, you won't see a Module1. That's why they have to be managed exclusively in Visual Studio or the VS macros IDE.) But you can also create a new module by right-clicking MyMacros and selecting Add New Item... in the VS macros IDE or New Module... in Visual Studio Macro Explorer. Macros are divided into projects containing modules, which in turn contain the individual macros.
I renamed the module as LineNumbers. I also renamed the subroutine as NumbersOn and copied the code to another one that I named NumbersOff. Changing True to False in the second one completes the code.
The illustration shows the completed VS macros.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
You can run a VS macro in several different ways:
- With a keyboard shortcut or toolbar icon in Visual Studio.
- In Macro Explorer
Double-click the VS macro icon. - In the Command window
For example, >Macros.MyMacros.LineNumbers.NumbersOn. Enter "m" to use Intellisense for the names of VS macros. If a VS macro needs a parameters and switch, you can pass it by running the VS macro in the Command window. - In the Macros IDE
This is a good way to run VS macros in Debug mode. Execution starts at the location of the cursor.

