Share via

How to: Use Add-ins to Control Macros

The Macros object in the Visual Studio automation model gives you a measure of programmatic control over macros being recorded in the integrated development environment (IDE). By using it, you can:

  • Pause or resume the macro recorder.

  • Add one or more lines of code to the macro being recorded.

  • Determine whether the macro recorder is currently recording a macro.

You can also use the ExecuteCommand method to directly issue a command to the IDE, such as creating a new file.


The Macros object members are not designed to be used within a macro; they must only be used within an Add-in.

The Visual Basic example below demonstrates how to reference and use the various members of the Macros automation model.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio Settings.


The following example checks to see whether a macro is currently being recorded. If it is, it adds an extra line of code to it.

Public Sub OnConnection(ByVal application As Object, _
  ByVal connectMode As ext_ConnectMode, ByVal addInInst _
  As Object, ByRef custom As Array) Implements _
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
End Sub

Public Sub macroTest(ByVal dte As DTE2)
        Dim objMac As Macros = dte.Macros
        ' Determine if a macro is recording. If it is, 
        ' add a line of code to it and then let it resume 
        ' recording.
        If objMac.IsRecording = True Then
            objMac.EmitMacroCode _
              ("MsgBox(""This was added by code."")")
            MsgBox("Macro is not recording.")
        End If
    Catch ex As Exception
    End Try
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

public void macroTest(DTE2 dte)
        Macros objMac = dte.Macros;
        // Determine if a macro is recording. If it is, 
        // add a line of code to it and then let it resume 
        // recording.
        if (objMac.IsRecording == true)
            objMac.EmitMacroCode("MsgBox(\"This was added by 
            System.Windows.Forms.MessageBox.Show("Macro is not 
    catch (Exception ex)

If you were to record a macro and open a text file, this is what the macro code would look like:

Public Module RecordingModule
    Sub TemporaryMacro()
        DTE.ItemOperations.NewFile("General\Text File")
    End Sub
End Module

When you run the example code and do the same thing, an extra line is emitted (added) to the macro code:

Public Module RecordingModule
    Sub TemporaryMacro()
        DTE.ItemOperations.NewFile("General\Text File")
        MsgBox("This line was added by code.")
    End Sub
End Module

See Also


How to: Change Window Characteristics

How to: Create an Add-In

Walkthrough: Creating a Wizard


Automating Repetitive Actions by Using Macros

Automation Object Model Chart

Other Resources

Creating and Controlling Environment Windows

Creating Add-ins and Wizards

Automation and Extensibility Reference