Download presentation
Presentation is loading. Please wait.
Published byMarshall Parker Modified over 9 years ago
1
Building COM Add-Ins Don Kiely Software Technologist Third Sector Technologies Fairbanks, Alaska 4-402
3
Me.About Software Technologist for Third Sector Technologies in Fairbanks, Alaska Software Technologist for Third Sector Technologies in Fairbanks, Alaska Develop software and Web applications Develop software and Web applications www.infoinsights.com www.thethirdsector.com Business and technology consulting Business and technology consulting
4
My Other Jobs Author Author Several books, including VB Programmer’s Guide to the Windows Registry Regular contributor to several publications, including Informant’s Microsoft Office & VBA Developer, VBPJ, and Information Week Training Training VB, VBA, VI, and SQL Server instructor for Application Developers Training Company
5
What Is A COM Add-in? ActiveX DLL or EXE with special registration ActiveX DLL or EXE with special registration Standard way of extending Office 2000 Standard way of extending Office 2000 Across all host applications Available both in user interface and Visual Basic for Applications environment Still need to deal with various object models Create with any COM development tool Create with any COM development tool
6
In the Past, Add-ins… Had different models across Office Had different models across Office Usually had a pre-defined file extension Usually had a pre-defined file extension Document-centric Had to be in a certain disk directory to appear in the Add-ins menu Had to be in a certain disk directory to appear in the Add-ins menu Or had to be listed in a special.INI file But now, the registry provides a common mechanism to discover installed add-ins.
7
Why Bother? Share code among multiple Office applications Share code among multiple Office applications Without the mess of Office 97 add-ins Maybe for uniform user interface tools Protect intellectual property Protect intellectual property Increased performance Increased performance It’s cool It’s cool Separates the developers from the mere power users
8
Add-Ins In The Registry HKEY_CURRENT_USER \Software \Microsoft\Office \ \AddIns HKEY_CURRENT_USER \Software \Microsoft\Office \ \AddIns Settings Settings ProgID: WordDocMgt.Connect FriendlyName: WordDocMgt LoadBehavior Sometimes others...
9
End-User Interface Add COM Add-Ins…menu item Add COM Add-Ins…menu item Old Tools|Add-ins… still supported LoadBehavior governs instantiation LoadBehavior governs instantiation 0: Not loaded 2: Loaded at startup 4: Load when run from command line (VB only) 8: Load on demand 16: Load on next startup (Office only) Then changes LoadBehavior to 8
10
Add-In Events OnConnection when added to host OnConnection when added to host User adds or host starts up Code to integrate add-in with host OnStartupComplete when host finished loading OnStartupComplete when host finished loading OnAddinstUpdate when any add-in added or removed in host OnAddinstUpdate when any add-in added or removed in host OnBeginShutDown when host begins shutdown OnBeginShutDown when host begins shutdown OnDisconnection when removed from host OnDisconnection when removed from host User removes manually or host shutting down Undo OnConnection work Plus, designer is a class module, so Initialize and Terminate events Plus, designer is a class module, so Initialize and Terminate events
11
Office Add-Ins Objects Application object Application object COMAddIns collection COMAddIn object Warning: AddIns collection for backward compatibility Warning: AddIns collection for backward compatibility
12
Building Add-In Objects Build with MOD or Visual Basic Build with MOD or Visual Basic More flexibility with Visual Basic DLL or EXE Property pages for host application Run standalone ActiveX ® designer ActiveX ® designer Optional, but convenient Manages registry settings for you Include one for each host Connect object used to load add-in Connect object used to load add-in Templates in MOD for VB, VC++, VJ++ Templates in MOD for VB, VC++, VJ++ Can’t use VB’s add-in project code for Office
13
Steps In VBA Develop in any host application Develop in any host application Create a new Visual Basic for Applications Add-in project Create a new Visual Basic for Applications Add-in project Add reference to Office object models you’ll use Add reference to Office object models you’ll use Fill in designer properties Fill in designer properties Export designer Export designer Import for each host application you’ll support Import for each host application you’ll support Change name, host Write event code Write event code
14
OnConnection Parameters Application Application ConnectMode ConnectMode ext_cm_Startup ext_cm_AfterStartup ext_cm_External ext_cm_CommandLine AddInInst AddInInst custom() As Variant custom() As Variant 1: how the host was started: standalone, embedded document, via automation 2+: depends on host
15
OnDisConnection parameters RemoveMode RemoveMode ext_dm_UserClosed Clean up host app’s user interface ext_dm_HostShutdown custom() As Variant custom() As Variant
16
A Few Details Put code specific to a host application in Designer; shared code in code or class module Put code specific to a host application in Designer; shared code in code or class module Must use class to trap WithEvents Package and Deployment Wizard Package and Deployment Wizard Properly registers add-ins for each host Ignores the Install Location you set Default becomes \Program Files\[add-in] If Load on Demand, disconnecting counts as a ‘demand’ If Load on Demand, disconnecting counts as a ‘demand’ OnConnection, AddinsUpdate, OnDisconnection will fire
17
Steps In Visual Basic With or without designer? With or without designer? Create new ActiveX DLL project Create new ActiveX DLL project Set reference to Microsoft Add-in Designer -- or -- Create new add-in project Create new add-in project Sets reference to extensibility object library Still need the VBA version Consider using template project Add reference to Office object models you’ll use Add reference to Office object models you’ll use Not necessarily host application’s object library Write event code Write event code
18
VB Application Design Without Designer Implement IDTExtensibility2 in a Connect class module Implement IDTExtensibility2 in a Connect class module With only one host, can early bind the application object in OnConnection With only one host, can early bind the application object in OnConnection How use add-in in multiple hosts? How use add-in in multiple hosts? Declare as Object: late bound Create Connect class for each host ConnectWord, ConnectExcel, etc. Single Connect, use TypeOf to test for type of Application object, and Not Is Nothing Then manually add registry entries Then manually add registry entries Register.vbp No free lunch No free lunch
19
VB Application Design With Designer Add one designer for each host app Add one designer for each host app Need to remove or modify most of the default code Need to remove or modify most of the default code It’s VB-specific Set designer’s Public property to True Implement AddinInstance instead of IDTExtensibility2 Implement AddinInstance instead of IDTExtensibility2 Remove Implements statement Automatically adds registry entries Automatically adds registry entries Kind of a free lunch Kind of a free lunch
20
Then What? Display a form in OnConnection Display a form in OnConnection Respond to events from host environment Respond to events from host environment Manipulate the host’s user interface Manipulate the host’s user interface Menu items, toolbar, etc. Does it really need a user interface? Whatever makes sense for the app Whatever makes sense for the app
21
Load Programmatically Application.COMAddins collection Application.COMAddins collection NOT the AddIns collection (if exists) One way: One way: Dim objAddIn As COMAddIn For Each objCAddIn In Application.COMAddIns If objAddIn.Description = "AddInName" Then If objAddIn.Description = "AddInName" Then If objAddIn.Connect = False Then If objAddIn.Connect = False Then objAddIn.Connect = True objAddIn.Connect = True COMAddIns.Update COMAddIns.Update Exit Sub Exit Sub End If End If Debug.Print objAddIn.GUID, objAddIn.ProgID Debug.Print objAddIn.GUID, objAddIn.ProgIDNext
22
Debugging Your Add-In Easiest with ActiveX Designer Easiest with ActiveX Designer Visual Basic manages registry Things get messy when you stop execution Things get messy when you stop execution OnDisconnection doesn’t fire Remove using Add-in Manager
23
Add-ins For VBE Uses Microsoft Visual Basic for Applications Extensibility Uses Microsoft Visual Basic for Applications Extensibility Different from Visual Basic’s extensibility library But most code can be duplicated VBE Objects VBE Objects VBProject VBComponent VBE (top-level)
24
Questions? Thanks for attending! Please remember to fill out the evaluation forms! How can I make this a better presentation? Don Kiely donkiely@computer.org Third Sector Technologies
25
Appendix A: Ideas The Simple The Simple Save all open documents at once Close all open document windows Insert MapPoint map in document The Sophisticated The Sophisticated Implement logging of user activity (hook into host application events) Modify the application response to events and user actions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.