Export (0) Print
Expand All

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.

NoteNote:

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.

NoteNote:

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 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 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

Community Additions

ADD
Show:
© 2015 Microsoft