How to create and load add-ins in the Visual Studio .NET IDE by using Visual Basic .NET (317345)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
This article was previously published under Q317345 For a Microsoft Visual Basic 6.0 version of this
article, see
189468. IN THIS TASKSUMMARY This article demonstrates 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 into the Visual Studio .NET interface.
back to the top
Requirements The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Visual Studio .NET
- Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000
Server, Microsoft Windows XP Professional, Microsoft Windows XP Server with
Microsoft .NET Framework
back to the top
Create and Load Add-ins in Visual Studio .NETCreate the Visual Basic Project- On the Start menu, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click New Project.
- Under Project Types, click to expand Other Projects, and then click Extensibility Projects.
- Under Templates, click Visual Studio .NET Add-in.
- In the Name text box, type InsertDateTime.
- 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 Basic, and then click Next.
- On the Select an Application Host page,
clear the Microsoft VSMacros IDEcheck box, and then click Next.
- 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, 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, 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.
- On the Summary page, click Finish. This creates a solution with both an add-in project and an
add-in setup project.
back to the top
Update the Connect.vb File and Add Custom Actions- In Solution Explorer, double-click Connect.vb, and then scroll through the file. Notice that Visual Studio .NET
inserts the necessary code templates by default. For this article, you only
need to do the following:
- Add a subroutine that writes the date and the
time.
- Modify the Exec subroutine.
- Below the Public Sub Exec subroutine, add the following code:
Public Function InsertDateTime() As Boolean
If Not IsNothing(applicationObject.ActiveDocument) Then
CType(applicationObject.ActiveDocument.Selection, TextSelection).Text = DateTime.Now.ToString
End If
Return True
End Function
- In Public Sub Exec, change the following code
handled = True
to:
handled = InsertDateTime()
- In Solution Explorer, right-click InsertDateTimeSetup, point to View, and then click Custom Actions.
- Right-click Custom Actions, and then click Add Custom Action.
- Click Application Folder, and then click OK.
- Under InsertDateTime (Active), click Primary output, and then click OK. Notice that primary output appears in the Install, the Commit, the Rollback, and the Uninstall nodes within the Custom Actions node.
back to the top
Build and Install the Add-in Project- Because setup projects are not included in the build
configuration by default, you must use one of the following methods to build
the solution:
- Right-click InsertDateTime, and then click Build. Similarly, right-click InsertDateTimeSetup, and then click Build.
- To build the entire solution at once, click Configuration Manager on the Build menu. Select the Build check box for InsertDateTimeSetup.
- Press the CTRL+SHIFT+B key combination to build the entire
solution. A complete installation package is now available for InsertDateTime.
- To install the add-in that you just built, follow these
steps:
- Close all instances of Visual Studio .NET, and then
save any changes if you are prompted.
- Open Windows Explorer, and then browse to the following
folder:
C:\InsertDateTime\InsertDateTimeSetup\Debug
- Right-click InsertDateTimeSetup.msi, and then click Install.
- In the InsertDateTimeSetup dialog box, click Next three times. Notice that a progress bar appears while the service
installs.
- After the add-in is installed, click Close.
- Restart Visual Studio .NET.
NOTE: After you restart Visual Studio .NET, the add-in is always
loaded until you uninstall it.
back to the top
Add an Icon to the Toolbar It is helpful, as well as instructive, to integrate your add-in
more fully into the Visual Studio integrated development environment (IDE). To
do this, add an icon to the toolbar, and then associate the add-in with a
keyboard shortcut.
- On the Tools menu, click Customize.
- On the Commands tab, in the Categories list, click Addins.
- Drag InsertDateTime to an active toolbar, and then click Keyboard.
- In the Show commands containing text box,
type insertdate. Notice that your add-in, named InsertDateTime.Connect.InsertDateTime, appears in the list below.
- 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.
- Press the BACKSPACE key to delete the key combination.
Press the CTRL+SHIFT+BACKSPACE key combination. Because no other command uses
this keyboard shortcut, click Assign.
- When you are prompted, click Yes, and then type any name for the new keyboard scheme. This new
keyboard scheme is a copy of the default scheme with the new add-in
mapping.
- Click Assign, and then click OK.
- In the Customize dialog box, click Close.
- In the text file, click InsertDateTime on the toolbar. Notice that the date and the time are inserted.
Alternately, you can press CTRL+SHIFT+BACKSPACE to achieve the same
results.
back to the top
Complete Code Listing (Connect.vb)
Imports Microsoft.Office.Core
imports Extensibility
imports System.Runtime.InteropServices
Imports EnvDTE
#Region " Read me for Add-in installation and setup information. "
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, if the Add-in becomes unavailable for reasons such as:
' 1) You moved this project to a computer other than which it was originally created on.
' 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
' 3) Registry corruption.
' you will need to re-register the Add-in by building the InsertDateTimeSetup project
' by right clicking the project in the Solution Explorer, then choosing install.
#End Region
<GuidAttribute("0045A524-BA5F-43F4-890B-C729D9A12FDB"), ProgIdAttribute("InsertDateTime.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Implements IDTCommandTarget
Dim applicationObject As EnvDTE.DTE
Dim addInInstance as EnvDTE.AddIn
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = CType(application, EnvDTE.DTE)
addInInstance = CType(addInInst, EnvDTE.AddIn)
If connectMode = Extensibility.ext_ConnectMode.ext_cm_UISetup Then
Dim objAddIn As AddIn = CType(addInInst, AddIn)
Dim CommandObj As Command
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, the Add-in or its commands may become unavailable for reasons such as:
' 1) You moved this project to a computer other than which it was originally created on.
' 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
' 3) You add new commands or modify commands already defined.
' You will need to re-register the Add-in by building the InsertDateTimeSetup project,
' right-clicking the project in the Solution Explorer, and then choosing install.
' Alternatively, you could execute the ReCreateCommands.reg file the Add-in Wizard generated in
' the project directory, or run 'devenv /setup' from a command prompt.
Try
CommandObj = applicationObject.Commands.AddNamedCommand(objAddIn, "InsertDateTime", "InsertDateTime", "Executes the command for InsertDateTime", True, 59, Nothing, 1 + 2) '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
CommandObj.AddControl(applicationObject.CommandBars.Item("Tools"))
Catch e As System.Exception
End Try
End If
End Sub
Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
handled = False
If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
If cmdName = "InsertDateTime.Connect.InsertDateTime" Then
handled = InsertDateTime()
Exit Sub
End If
End If
End Sub
Public Function InsertDateTime() As Boolean
If Not IsNothing(applicationObject.ActiveDocument) Then
CType(applicationObject.ActiveDocument.Selection, TextSelection).Text = DateTime.Now.ToString
End If
Return True
End Function
Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef statusOption As vsCommandStatus, ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus
If neededText = EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
If cmdName = "InsertDateTime.Connect.InsertDateTime" Then
statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
Else
statusOption = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
End Class
back to the top
Verify That It Works- Press the CTRL+N key combination.
- In the New File dialog box, click General under Categories, click Text File under Templates, and then click Open.
- On the Tools menu, click Insert Date and Time. The current
date and time are written to the text file.
back to the top
Uninstall the Add-in- Close all instances of the Visual Studio .NET
environment.
- Open Windows Explorer, and then browse to the following
folder:
C:\InsertDateTime\InsertDateTimeSetup\Debug
- Right-click InsertDateTimeSetup.msi, and then click Uninstall.
- Click Yes when you are prompted to uninstall.
back to the top
Troubleshooting
back to the top
REFERENCES For more information, refer to the following Microsoft Web sites:
back to the top
Modification Type: | Major | Last Reviewed: | 3/4/2006 |
---|
Keywords: | kbvs2005doesnotapply kbvs2005swept kbHOWTOmaster KB317345 kbAudDeveloper |
---|
|