# Visual Basic > Visual Basic FAQs >  [FAQ's: OD] How do I use VB 6 to make an Add-In for an Office application?

## RobDog888

You can create an Add-In two ways (First method recommended for beginner VB programmers):
The Add-In Project template (Fig. 1a) but that adds allot of unnecessary code to be deleted and form to be removed. 
The other is just to create an ActiveX DLL project (Fig. 1b) and add the necessary references and a copy of the "AddIn.Dsr" Designer form.
Add a reference to the Office program you want your AddIn to be forAdd a reference to "Microsoft Office" (for any commandbar access)Add a reference to "Microsoft AddIn Designer"Add a reference to "Microsoft Visual Basic Extensibility 6.0"Copy the "ADDIN.DSR" designer form found in "?:\Program Files\Microsoft Visual Studio\VB98\Template\Projects" and paste it into your current project folder so you are NOT working with the actual original template file.Add the new AddIn form "ADDIN.DSR" to your ActiveX DLL project by right clicking your project in the project explorer and browsing to the directory where you just copied over the ADDIN.DSR form and click OK.Remove Class1.cls from your ActiveX project.
Now you used AddIn project template from th first chioce then we will need to need to remove the form "frmAddIn" and delete all the code. If you used the second method then you will need to delete all the code and remove the Class1.cls file from your project.

To the best of my knowledge there is no difference between the two methods other then preparation for your use like shown in this FAQ.


Fig. 1






Once we have our project setup and ready to receive code and any form(s)/controls we will want to fill in and choose our designer options as shown in Fig.2 below.

Fig. 2





After we have done that you will want to start coding your Add-In. The first step will be to add the IDTExtensibility2 implementation and add all five (5) event procedures. Even if your not going to use them all you still need to have them declared for full compatibility. Your code behind the AddIn designer should look like this...


VB Code:
Option Explicit
 Implements IDTExtensibility2
 Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
 End Sub
 Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
 End Sub
 Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
 End Sub
 Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
custom() As Variant)
 End Sub
 Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
 End Sub

Next we will want to start adding in c0d for our AddIn to do something. For this FAQ I will keep it simple and create a Toolbar Button that when clicked displays a message box.



VB Code:
Option Explicit
'Added references:
'Microsoft Office xx.0 Object Library
'Microsoft Outlook xx.0 Object Library
'Microsoft AddIn Designer
'Microsoft Visual Basic 6.0 Extensibility
Implements IDTExtensibility2
 Public moApp As Outlook.Application
Public moAppInst As Object
 Public WithEvents moCBMeow As Office.CommandBarButton
 Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
    '<PLACEHOLDER - I AM NOT USING IT FOR DEMO, ONLY FOR COMPATIBILITY STANDARDS>
    'The OnAddInsUpdate method is called when a change occurs to the list of add-ins in the COM Add-Ins dialog box,
    'such as when an add-in is loaded or unloaded. The custom parameter is an array that can be used to provide
    'additional data to the OnAddInsUpdate method if desired.
End Sub
 Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
    'The OnBeginShutdown method is called while the environment is being shut down. The custom parameter is an array
    'that can be used to provide additional data to the OnBeginShutdown method if desired.
    If TypeName(moCBMeow) <> "Nothing" Then
        moCBMeow.Delete
    End If
    Set moCBMeow = Nothing
End Sub
 Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
    ByVal AddInInst As Object, custom() As Variant)
    '<INITIAL EVENT THAT FIRES WHEN TEH ADDIN IS LOADED>
    '<SET THE PUBLIC APPLICATION OBJECT TO THE PASSED IN INSTANCE FOR SECURITY AND TRUST>
    'The OnConnection method is called when the add-in is loaded into the environment. The addInInst parameter is an
    'object that represents the instance of the managed COM add-in. The custom parameter is an array that can be used
    'to use to provide additional data to the OnConnection method if desired. The application parameter represents the
    'host application. The connectMode parameter is an ext_cm constant that indicates how the managed COM add-in was loaded.
    Set moApp = Application
    Set moAppInst = AddInInst
    '<IF YOU ARE NOT IN STARTUP THEN MANUALLY CALL ONSTARTUPCOMPLETE>
    If (ConnectMode <> AddInDesignerObjects.ext_ConnectMode.ext_cm_Startup) Then Call IDTExtensibility2_OnStartupComplete(custom)
End Sub
 Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
    custom() As Variant)
    'The OnDisconnection method is called when the managed COM add-in is unloaded, such as when the user closes the
    'host application. The custom parameter is an array that can be used to provide additional data to the OnDisconnection
    'method if desired. The RemoveMode parameter is an ext_dm constant that indicates how the managed COM add-in was unloaded.
    If TypeName(moCBMeow) <> "Nothing" Then
        moCBMeow.Delete
    End If
    Set moCBMeow = Nothing
End Sub
 Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
    '<SET OUT TOLBAR BUTTON IN THIS EVENT AS ITS THE LAST TO FIRE SO OUTLOOK WILL BE COMPLETELY LOADED AND STARTED>
    'The OnAction property is optional but recommended. It should be set to the ProgID of the add-in, so that if
    'the add-in is not loaded when a user clicks the button, MSO loads the add-in automatically and then raises
    'the Click event for the add-in to handle.
    Set moCBMeow = moApp.ActiveExplorer.CommandBars.Item("Standard").FindControl(, , "890", False, True)
    If TypeName(moCBMeow) = "Nothing" Then
        Set moCBMeow = moApp.ActiveExplorer.CommandBars.Item("Standard").Controls.Add(msoControlButton, , "890", , True)
    End If
    With moCBMeow
        .BeginGroup = True
        .Caption = "Meow"
        .DescriptionText = "Meow meow meow"
        .Enabled = True
        .OnAction = "!<RobDog888.Connect>"
        Clipboard.Clear
        Clipboard.SetData LoadPicture("C:\Cat.bmp")
        .PasteFace
        .Style = msoButtonIconAndCaption
        .Tag = "890"
        .ToolTipText = "Meow meow meow"
        .Visible = True
    End With
 End Sub
 Private Sub moCBMeow_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    '<OUR TOOLBAR BUTTON CLICK EVENT PROCEDURE>
    MsgBox "Meow meow meow!", vbOKOnly + vbInformation, "RobDog888's VB 6 Add-In Outlook FAQ"
End Sub

Now once we compile this project it wil produce a .dll file that is our AddIn. We can go into Outlook and add it - Click Tools > Options > Other tab > Advanced Options... > COM AddIns > Add... > browse to the location of your .dll AddIn file > click OK > click OK > click OK > click OK > click OK. Then you will see your new custom Toolbar button (Fig. 3).


Fig. 3





Then the resulting MsgBox when clicked.  :Smilie: 




You can have your Add-In do just about anything in any of the Office Applications.  :Wink: 

For more information on Office Automation please see this thread - "How do I automate an Office App using VB 6?"


New added zip project example.

----------


## Krammig

Rob thanks for this tutorial of 12 years ago .....  

A question please, opening my connect.dsr here it shows Application Version - Microsoft Outlook 15.0. This is fine I guess as that is what is installed now, in this case Office 2013 (I just installed this). Previously it was showing Microsoft Outlook 11. 

The question I have is, if the User has say Office 2016 installed for example, and then at some point even upgrades that, will my plugin still work for them given it is showing Microsoft Outlook 15.0 in this compile ?

Does it just automatically upgrade/handle this internally ?  or is there something I should be doing to cover this ?

cheers

----------

