[FAQ's: OD] How do I use VB 6 to make an Add-In for an Office application?-VBForums
Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I use VB 6 to make an Add-In for an Office application?

Hybrid View

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,164

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

    You can create an Add-In two ways (First method recommended for beginner VB programmers):
    1. The Add-In Project template (Fig. 1a) but that adds allot of unnecessary code to be deleted and form to be removed.

    2. 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 for
      • Add 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:
    1. Option Explicit
    2.  
    3. Implements IDTExtensibility2
    4.  
    5. Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
    6.  
    7. End Sub
    8.  
    9. Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
    10.  
    11. End Sub
    12.  
    13. Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
    14. ByVal AddInInst As Object, custom() As Variant)
    15.  
    16. End Sub
    17.  
    18. Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
    19. custom() As Variant)
    20.  
    21. End Sub
    22.  
    23. Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
    24.  
    25. 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:
    1. Option Explicit
    2. 'Added references:
    3. 'Microsoft Office xx.0 Object Library
    4. 'Microsoft Outlook xx.0 Object Library
    5. 'Microsoft AddIn Designer
    6. 'Microsoft Visual Basic 6.0 Extensibility
    7. Implements IDTExtensibility2
    8.  
    9. Public moApp As Outlook.Application
    10. Public moAppInst As Object
    11.  
    12. Public WithEvents moCBMeow As Office.CommandBarButton
    13.  
    14. Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
    15.     '<PLACEHOLDER - I AM NOT USING IT FOR DEMO, ONLY FOR COMPATIBILITY STANDARDS>
    16.     'The OnAddInsUpdate method is called when a change occurs to the list of add-ins in the COM Add-Ins dialog box,
    17.     'such as when an add-in is loaded or unloaded. The custom parameter is an array that can be used to provide
    18.     'additional data to the OnAddInsUpdate method if desired.
    19. End Sub
    20.  
    21. Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
    22.     'The OnBeginShutdown method is called while the environment is being shut down. The custom parameter is an array
    23.     'that can be used to provide additional data to the OnBeginShutdown method if desired.
    24.     If TypeName(moCBMeow) <> "Nothing" Then
    25.         moCBMeow.Delete
    26.     End If
    27.     Set moCBMeow = Nothing
    28. End Sub
    29.  
    30. Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
    31.     ByVal AddInInst As Object, custom() As Variant)
    32.     '<INITIAL EVENT THAT FIRES WHEN TEH ADDIN IS LOADED>
    33.     '<SET THE PUBLIC APPLICATION OBJECT TO THE PASSED IN INSTANCE FOR SECURITY AND TRUST>
    34.     'The OnConnection method is called when the add-in is loaded into the environment. The addInInst parameter is an
    35.     'object that represents the instance of the managed COM add-in. The custom parameter is an array that can be used
    36.     'to use to provide additional data to the OnConnection method if desired. The application parameter represents the
    37.     'host application. The connectMode parameter is an ext_cm constant that indicates how the managed COM add-in was loaded.
    38.     Set moApp = Application
    39.     Set moAppInst = AddInInst
    40.     '<IF YOU ARE NOT IN STARTUP THEN MANUALLY CALL ONSTARTUPCOMPLETE>
    41.     If (ConnectMode <> AddInDesignerObjects.ext_ConnectMode.ext_cm_Startup) Then Call IDTExtensibility2_OnStartupComplete(custom)
    42. End Sub
    43.  
    44. Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
    45.     custom() As Variant)
    46.     'The OnDisconnection method is called when the managed COM add-in is unloaded, such as when the user closes the
    47.     'host application. The custom parameter is an array that can be used to provide additional data to the OnDisconnection
    48.     'method if desired. The RemoveMode parameter is an ext_dm constant that indicates how the managed COM add-in was unloaded.
    49.     If TypeName(moCBMeow) <> "Nothing" Then
    50.         moCBMeow.Delete
    51.     End If
    52.     Set moCBMeow = Nothing
    53. End Sub
    54.  
    55. Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
    56.     '<SET OUT TOLBAR BUTTON IN THIS EVENT AS ITS THE LAST TO FIRE SO OUTLOOK WILL BE COMPLETELY LOADED AND STARTED>
    57.     'The OnAction property is optional but recommended. It should be set to the ProgID of the add-in, so that if
    58.     'the add-in is not loaded when a user clicks the button, MSO loads the add-in automatically and then raises
    59.     'the Click event for the add-in to handle.
    60.     Set moCBMeow = moApp.ActiveExplorer.CommandBars.Item("Standard").FindControl(, , "890", False, True)
    61.     If TypeName(moCBMeow) = "Nothing" Then
    62.         Set moCBMeow = moApp.ActiveExplorer.CommandBars.Item("Standard").Controls.Add(msoControlButton, , "890", , True)
    63.     End If
    64.     With moCBMeow
    65.         .BeginGroup = True
    66.         .Caption = "Meow"
    67.         .DescriptionText = "Meow meow meow"
    68.         .Enabled = True
    69.         .OnAction = "!<RobDog888.Connect>"
    70.         Clipboard.Clear
    71.         Clipboard.SetData LoadPicture("C:\Cat.bmp")
    72.         .PasteFace
    73.         .Style = msoButtonIconAndCaption
    74.         .Tag = "890"
    75.         .ToolTipText = "Meow meow meow"
    76.         .Visible = True
    77.     End With
    78.  
    79. End Sub
    80.  
    81. Private Sub moCBMeow_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    82.     '<OUR TOOLBAR BUTTON CLICK EVENT PROCEDURE>
    83.     MsgBox "Meow meow meow!", vbOKOnly + vbInformation, "RobDog888's VB 6 Add-In Outlook FAQ"
    84. 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.




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

    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.
    Attached Files Attached Files
    Last edited by RobDog888; Oct 26th, 2006 at 05:18 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.