How to: Compile and Run the Automation Object Model Code Examples
The code examples found in the Automation object model reference topics are designed to run in an add-in created by the Add-in Wizard. In addition, the Visual Basic code examples can run as a macro. The following sections explain how to compile and run the Automation object model code examples.
To run a code sample in an add-in
Create an add-in by using the Add-in Wizard.
For more information, see How to: Create an Add-In.
Add the code example to the add-in's Connect class.
Call the code example procedure from the add-in's OnConnection method. If the code example requires the DTE2 object as an argument, pass in the _applicationObject member variable, which is declared in the add-in code. The following code shows the relevant portions of the Connect class from both a Visual Basic and Visual C# add-in:
Public Sub OnConnection(ByVal application As Object, ... ) _ Implements IDTExtensibility2.OnConnection _applicationObject = CType(application, EnvDTE80.DTE2) . . . ' Pass the _applicationObject member variable to the code ' example. MyCodeExample(_applicationObject) End Sub ' Other methods in the Connect class. . . . ' This is the code example that was added to the Connect class. Sub MyCodeExample(ByVal dte As DTE2) . . . End Sub
public void OnConnection(object application, ... , ref Array custom) { _applicationObject = (DTE2)application; . . . // Pass the _applicationObject member variable to the code // example. MyCodeExample(_applicationObject); } // Other methods in the Connect class. . . . // This is the code example that was added to the Connect class. void MyCodeExample(DTE2 dte) { . . . }
For Visual C# add-ins, add a reference to the System.Windows.Forms assembly by clicking the add-in project's name in Solution Explorer, selecting Add Reference from the Project menu, and choosing System.Windows.Forms.dll in the Add Reference dialog box.
For Visual C# add-ins, insert the following code at the top of the Connect.cs file:
using System.Windows.Forms;
Compile the add-in by selecting Build Solution from the Build menu.
Select Add-in Manager from the Tools menu.
The Add-in Manager dialog box appears.
Load the add-in by selecting the check box next to the add-in's name in the Add-in Manager dialog box, and click OK.
To run a Visual Basic code example as a macro
On the Tools menu, point to Macros, and then click Macros IDE.
The Macros IDE appears.
On the View menu of the Macros IDE, click Project Explorer.
Expand the MyMacros node in Project Explorer.
Double-click Module1.
Add the code example to the module file.
If the code example requires the DTE object as an argument, create a macro without parameters that passes the global DTE variable to the code example. The following code shows how:
' Visual Basic ' Macro without parameters that calls the code example. Sub CodeExample() CodeExample(DTE) End Sub ' Original code example. Sub CodeExample(ByVal dte As DTE2) . . . End Sub
Close the Macros IDE.
On the Tools menu in the Visual Studio IDE, point to Macros, and then click Macro Explorer.
The Macro Explorer window appears.
In Macro Explorer, expand the MyMacros node, and then expand the Module1 node.
In Macro Explorer, right-click the code example macro name and select Run from the shortcut menu.
See Also
Tasks
How to: Control Add-Ins By Using the Add-In Manager
How to: Edit and Programmatically Create Macros