Microsoft KB Archive/816167

= How to create and load add-ins in Visual Studio .NET by using Visual C# .NET or in Visual Studio 2005 by using Visual C# 2005 =

Article ID: 816167

Article Last Modified on 1/11/2007

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 317345.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Create and Load Add-ins in Visual Studio .NET
 * Complete the Steps in the Extensibility Wizard
 * Update the Connect.cs File and Add Custom Actions
 * Build and Install the Add-in Project
 * Add an Icon to the Toolbar
 * Complete Code Listing
 * Verify That It Works
 * Remove the Add-in
 * Troubleshooting
 * REFERENCES



SUMMARY
This article describes how to create a simple, compiled add-in that inserts the current date and time at the insertion point. In this article, you create and install an add-in project, load the add-in, and integrate the add-in in the Visual Studio .NET or Visual Studio 2005 interface.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you must have:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Windows 2000 Professional, Microsoft Windows Server 2003, Microsoft Windows 2000 Server, Microsoft Windows XP Professional, Microsoft Windows XP Server with the Microsoft .NET Framework

back to the top

Create and Load Add-ins in Visual Studio .NET

 * 1) Start Visual Studio .NET or Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, expand Other Projects, and then click Extensibility Projects.

Note In Visual Studio 2005, expand Other Projects Types under Project Types, and then click Extensibility.
 * 1) Under Templates, click Visual Studio .NET Add-in or Visual Studio Add-in.
 * 2) In the Name text box, type InsertDateTime.
 * 3) In the Location text box, type C:\, and then click OK. This starts the Extensibility Wizard.

back to the top

Complete the Steps in the Extensibility Wizard
 On the first page of the Extensibility Wizard, click Next. On the Select a Programming Language page, click Create an Add-in using Visual C#, and then click Next. On the Select a Application Host page, clear the Microsoft VSMacros IDE check box, and then click Next.

Note In Visual Studio 2005, you do not have to click to clear the Microsoft VSMacros IDE check box. On the Enter a Name and Description page, type Insert Date and Time in the Name text box. In the Description text box, type Inserts the current date and time, and then click Next. On the Choose Add-in Options page, click to select the following check box to create an item on the Tools menu:

'''Yes, create a 'Tools' menu item. By default this will cause the Add-in to load when the button is clicked unless the Add-in is set to load on startup of the host application.'''

 On the Choose Add-in Options page, click to select the following check box so that the add-in loads when the host application loads:

I would like my Add-in to load when the host application starts.

Click Next. On the Choosing 'Help About' Information page, click Next.</li> On the Summary page, click Finish. This creates a solution with both an add-in project and an add-in setup project.</li></ol>

back to the top

Update the Connect.cs File and Add Custom Actions
<ol> In Solution Explorer, double-click Connect.cs, and then scroll through the file. Notice that by default, Visual Studio .NET and Visual Studio 2005 insert the necessary code templates. For this article, you only need to do the following: <ol style="list-style-type: lower-alpha;"> Add a method that writes the date and time.</li> Modify the Exec method.</li></ol> </li>  Add the following code after the Exec method: public bool InsertDateTime {   if(applicationObject.ActiveDocument != null) ((TextSelection)applicationObject.ActiveDocument.Selection).Text = DateTime.Now.ToString; return true; } </li>  In the Exec method, change the following code: handled = true;

to the following: handled = InsertDateTime; </li> In Solution Explorer, right-click InsertDateTimeSetup, point to View, and then click Custom Actions.</li> Right-click Custom Actions, and then click Add Custom Action.</li> Click Application Folder, and then click OK.</li> Click Primary output from InsertDateTime(Active), and then click OK. Notice that primary output appears in the Install, the Commit, the Rollback, and the Uninstall nodes under the Custom Actions node.</li></ol>

back to the top

Build and Install the Add-in Project
<ol> Because setup projects are not included in the build configuration by default, you must use one of the following methods to build the solution: <ul> Method 1: Right-click InsertDate, and then click Build. Similarly, right-click InsertDateTimeSetup, and then click Build.</li> Method 2: To build the whole solution at once, click Configuration Manager on the Build menu. Click to select the Build check box for InsertDateTimeSetup.</li></ul> </li> Press the CTRL+SHIFT+B key combination to build the whole solution. A complete installation package is now available for InsertDateTime.</li> Install the add-in that you just built. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Close all instances of Visual Studio .NET or Visual Studio 2005, and then save any changes if you are prompted.</li> <li>Open Windows Explorer, and then locate the following folder:

C:\InsertDateTime\InsertDateTimeSetup\Debug</li> <li>Right-click InsertDateTimeSetup.msi, and then click Install.</li> <li>In the InsertDateTimeSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.</li> <li>After the add-in is installed, click Close.</li> <li>Restart Visual Studio .NET or Visual Studio 2005.

Note After you restart Visual Studio .NET or Visual Studio 2005, the add-in is always loaded until you remove it.</li></ol> </li></ol>

back to the top

Add an Icon to the Toolbar
It is helpful, and also instructive, to integrate your add-in more fully in the Visual Studio IDE. To do this, add an icon to the toolbar, and then associate the add-in with a keyboard shortcut.
 * 1) On the Tools menu, click Customize.
 * 2) On the Commands tab, click Addins in the Categories list.
 * 3) Drag InsertDateTime to the active toolbar, and then click Keyboard.
 * 4) In the Show commands containing text box, type insertdatetime . Notice that your add-in (InsertDateTime.Connect.InsertDateTime) appears in the list.
 * 5) In the Press shortcut key(s) text box, press the CTRL+SHIFT+RIGHT ARROW key combination. Notice that the Shortcut currently used by text box notifies you that the Edit.SizeControlRight command already uses this keyboard shortcut.
 * 6) Press BACKSPACE to delete the key combination. Press the CTRL+SHIFT+BACKSPACE key combination. Because no other command uses this keyboard shortcut, click Assign.
 * 7) In the Options dialog box, click OK.
 * 8) In the Customize dialog box, click Close.
 * 9) In a text file, click InsertDateTime on the toolbar. Notice that the text is inserted. Alternatively, you can also press the CTRL+SHIFT+BACKSPACE key combination to insert the text.

back to the top

Complete Code Listing
Connect.cs namespace InsertDateTime {   using System; using Microsoft.Office.Core; using Extensibility; using System.Runtime.InteropServices; using EnvDTE; [GuidAttribute(&quot;65EA989D-DFCD-4D36-9E00-A9D928F8B9A2&quot;), ProgId(&quot;InsertDateTime.Connect&quot;)] public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget {       public Connect {       }

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) {           applicationObject = (_DTE)application; addInInstance = (AddIn)addInInst; if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup) {               object []contextGUIDS = new object[] { }; Commands commands = applicationObject.Commands; _CommandBars commandBars = applicationObject.CommandBars;

try {                   Command command = commands.AddNamedCommand(addInInstance, &quot;InsertDateTime&quot;, &quot;InsertDateTime&quot;, &quot;Executes the command for InsertDateTime&quot;, true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled); CommandBar commandBar = (CommandBar)commandBars[&quot;Tools&quot;]; CommandBarControl commandBarControl = command.AddControl(commandBar, 1); }               catch(System.Exception /*e*/) {               }            }        }

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) {       }

public void OnAddInsUpdate(ref System.Array custom) {       }

public void OnStartupComplete(ref System.Array custom) {       }

public void OnBeginShutdown(ref System.Array custom) {       }        public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText) {           if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) {               if(commandName == &quot;InsertDateTime.Connect.InsertDateTime&quot;) {                   status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled; }           }        }

public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) {           handled = false; if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault) {               if(commandName == &quot;InsertDateTime.Connect.InsertDateTime&quot;) {                   handled = InsertDateTime; return; }           }        }

public bool InsertDateTime {           if(applicationObject.ActiveDocument != null) ((TextSelection)applicationObject.ActiveDocument.Selection).Text = DateTime.Now.ToString; return true; }       private _DTE applicationObject; private AddIn addInInstance; } } back to the top

Verify That It Works

 * 1) Press CTRL+N.
 * 2) In the New File dialog box, click General under Categories, click Text File under Templates, and then click Open.
 * 3) On the Tools menu, click InsertDateTime. The current date and time are written to the text file.

back to the top

Remove the Add-in

 * 1) Close all instances of Visual Studio .NET or Visual Studio 2005.
 * 2) Open Windows Explorer, and then locate the following folder:

C:\InsertDateTime\InsertDateTimeSetup\Debug
 * 1) Right-click InsertDateTimeSetup.msi, and then click Uninstall.
 * 2) Click Yes when you are prompted to uninstall.

back to the top

Troubleshoot
<ul> <li>Add-ins are compiled, and entries are written to the registry. Therefore, whenever you change the add-in project, you must recompile, and then reinstall the add-in.</li> <li>When you try to build the project, you may receive one of the following error messages:

unable to write to output file

-or-

file in use

To resolve this problem, right-click the setup project in Solution Explorer, and then click Uninstall to remove the add-in. You can the rebuild both projects, and reinstall the add-in.</li></ul>

back to the top

<div class="references_section">