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.

Note

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.

Note

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 Export Settings on the Tools menu. For more information, see Working with Settings.

Example

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 _
  IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    macroTest(_applicationObject)
End Sub

Public Sub macroTest(ByVal dte As DTE2)
    Try
        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."")")
        Else
            MsgBox("Macro is not recording.")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    macroTest(_applicationObject);
}

public void macroTest(DTE2 dte)
{
    try
    {
        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 code.\")");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show(
              "Macro is not recording.");
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

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

Tasks

How to: Change Window Characteristics

How to: Create an Add-In

Walkthrough: Creating a Wizard

Concepts

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