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 translations Article translations
Article ID: 816167 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

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

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.
  4. Under Templates, click Visual Studio .NET Add-in or Visual Studio Add-in.
  5. In the Name text box, type InsertDateTime.
  6. In the Location text box, type C:\, and then click OK. This starts the Extensibility Wizard.

Complete the Steps in the Extensibility Wizard

  1. On the first page of the Extensibility Wizard, click Next.
  2. On the Select a Programming Language page, click Create an Add-in using Visual C#, and then click Next.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. On the Choosing 'Help About' Information page, click Next.
  8. On the Summary page, click Finish. This creates a solution with both an add-in project and an add-in setup project.

Update the Connect.cs File and Add Custom Actions

  1. 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:
    1. Add a method that writes the date and time.
    2. Modify the Exec method.
  2. 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;
    }
    
  3. In the Exec method, change the following code:
    handled = true;

    to the following:
    handled = InsertDateTime();
  4. In Solution Explorer, right-click InsertDateTimeSetup, point to View, and then click Custom Actions.
  5. Right-click Custom Actions, and then click Add Custom Action.
  6. Click Application Folder, and then click OK.
  7. 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.

Build and Install the Add-in Project

  1. Because setup projects are not included in the build configuration by default, you must use one of the following methods to build the solution:
    • Method 1: Right-click InsertDate, and then click Build. Similarly, right-click InsertDateTimeSetup, and then click Build.
    • 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.
  2. Press the CTRL+SHIFT+B key combination to build the whole solution. A complete installation package is now available for InsertDateTime.
  3. Install the add-in that you just built. To do this, follow these steps:
    1. Close all instances of Visual Studio .NET or Visual Studio 2005, and then save any changes if you are prompted.
    2. Open Windows Explorer, and then locate the following folder:

      C:\InsertDateTime\InsertDateTimeSetup\Debug
    3. Right-click InsertDateTimeSetup.msi, and then click Install.
    4. In the InsertDateTimeSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.
    5. After the add-in is installed, click Close.
    6. 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.

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.

Complete Code Listing

Connect.cs
namespace InsertDateTime
{
	using System;
	using Microsoft.Office.Core;
	using Extensibility;
	using System.Runtime.InteropServices;
	using EnvDTE;
	
	[GuidAttribute("65EA989D-DFCD-4D36-9E00-A9D928F8B9A2"), ProgId("InsertDateTime.Connect")]
	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, "InsertDateTime", "InsertDateTime", "Executes the command for InsertDateTime", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
					CommandBar commandBar = (CommandBar)commandBars["Tools"];
					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 == "InsertDateTime.Connect.InsertDateTime")
				{
					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 == "InsertDateTime.Connect.InsertDateTime")
				{
					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;
		
	}
}

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.

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
  3. Right-click InsertDateTimeSetup.msi, and then click Uninstall.
  4. Click Yes when you are prompted to uninstall.

Troubleshoot

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

REFERENCES

For more information, see the following Microsoft Web sites:
Automation Samples for Visual Studio .NET
http://msdn2.microsoft.com/en-us/vstudio/aa718336.aspx
Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
http://msdn.microsoft.com/msdnmag/issues/02/02/vside/

Properties

Article ID: 816167 - Last Review: January 11, 2007 - Revision: 3.4
APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbide kbhowtomaster KB816167

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com