Create COM add-ins in Visual Basic
With Microsoft Visual Basic version 6.0, you can use the same Add-In Designer to build a COM add-in that you would use to build a Visual Basic 6.0 add-in. This designer wraps the IDTExtensibility2 interface so that you do not have to implement it yourself. It also registers your add-in for you.
Note To create a single add-in that works with multiple Office applications, you must use the Visual Basic Implements statement when creating add-ins, because the Visual Basic Add-In Designer does not support multiple host applications in one .dll file.
Start Visual Basic 6.0, and on the New tab, click Addin, and then click Open.
A designer class (Connect) and a form (frmAddIn) are added to your project.
Note If the New Project dialog box doesn't appear when you first start Visual Basic, on the File menu, click New Project.
In the Project window, expand the Designers folder under MyAddIn, and then double-click Connect.
In the Application box, click Microsoft MapPoint, and then in the Application Version box, click either Microsoft MapPoint 9.0 (version 2002) or Microsoft MapPoint 11.0 (version 2004).
In the Initial Load Behavior box, click Startup, and then close the Add-In Designer by clicking the Close button.
On the Project menu, click References, select the check box next to Microsoft MapPoint 9.0 Object Library or Microsoft MapPoint 11.0, and then click OK.
In the Project window, right-click Connect, and then click View Code.
Select all the code in the Code window, and press DELETE.
That code was designed for and works with Visual Basic add-ins, not Office COM add-ins.
Add the following code:
Option Explicit Public m_formDisplayed As Boolean Public g_oApp As Mappoint.Application Dim m_frmAddIn As New frmAddIn Sub Hide() '--Hides the form and remembers its state On Error Resume Next m_formDisplayed = False m_frmAddIn.Hide End Sub Sub Show()'--Shows the form and remembers its state On Error Resume Next If m_frmAddIn Is Nothing Then Set m_frmAddIn = New frmAddIn End If Set m_frmAddIn.g_oApp = g_oApp Set m_frmAddIn.Connect = Me m_formDisplayed = True m_frmAddIn.Show vbModal End Sub '------------------------------------------------------ 'This method adds the add-in to MapPoint '------------------------------------------------------ Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant) On Error GoTo error_handler 'Save the MapPoint instance Set g_oApp = Application '--The following has the name of the menu item that is added: g_oApp.AddCommand "MapPoint Addin...", "Show", Me If ConnectMode = ext_cm_AfterStartup Then If GetSetting(App.Title, "Settings", "DisplayOnConnect", _ "0") = "1" Then 'Set this to display the form on connect Me.Show End If End If Exit Sub error_handler: MsgBox Err.Description End Sub '------------------------------------------------------ 'This method removes the add-in from MapPoint '------------------------------------------------------ Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant) On Error Resume Next 'Delete the commands that were added for this add-in g_oApp.RemoveCommands Me Unload m_frmAddIn Set m_frmAddIn = Nothing End Sub
In the Project window, right-click the frmAddIn item, and click View Code.
Remove the following code:
Public VBInstance As VBIDE.VBE
and
Private Sub OKButton_Click() MsgBox "AddIn operation on: " & VBInstance.FullName End Sub
In the general declarations part of the form, add a declaration for the MapPoint application as follows:
Public g_oApp As Mappoint.Application
This gives the add-in a reference to the proper instance of MapPoint in case there is more than one.
On the File menu, click Save, and then save the project.
On the File menu, click Make MyAddIn.dll.
The designer automatically registers the add-in for you.
Start MapPoint, and on the Tools menu, click MapPoint AddIn.
A blank form opens.
Notes
The following code from this sample is specific to MapPoint:
In the OnConnection event, the following line of code tells MapPoint to add a "MapPoint Addin..." menu item to the Tools menu:
g_oApp.AddCommand "MapPoint Addin...", "Show", Me
When that menu item is called, the method indicated in the second parameter (in this case the Show method) is called.
The third parameter is a reference to the object (in this case the Connect designer) that has the Show method. You could add multiple menu items with their own handlers by calling this method again.
In the OnDisconnection event, the following line of code removes any commands that have been added by this add-in:
g_oApp.RemoveCommands Me
Give your project a unique name by clicking MyAddInProperties on the Project menu, and then in the Project Name box, typing the name of your add-in. If you also change the name of the designer from Connect, remember to update the code that references it.
Related topics
About the Microsoft MapPoint object model