Contents
Overview 1
Introduction to COM Add-ins 2
Developing a COM Add-in 8
Working with a COM Add-in Project 20
Working with Command Bars 25
Creating Custom Property Pages for
Outlook 2000 42
Lab A: Creating the MoveApp COM
Add-in 50
Lab B: Converting a Visual Basic for
Applications Macro to a COM Add-in 58
Review 69
Module 10: Creating
COM Add-ins Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Copy Editor: Shari G. Smith (R & S Consulting)
Online Program Manager: Arlo Emerson (Aditi)
Production Support: Irene Barnett (Barnett Communications)
Manufacturing Manager: Bo Galford
Manufacturing Support: Mimi Dukes (S&T Onsite)
Development Services: Kimber Dodge
Lead Product Manager: Mary Larson
Group Product Manager: Robert Stewart Module 10: Creating COM Add-ins iii Instructor Notes Module 10: Creating COM Add-ins
This module presents Component Object Model (COM) add-ins as a way of
developing solutions for Microsoft
®
Outlook
®
2000 and other Microsoft
Office 2000 applications.
At the end of this module, students will be able to write, debug, compile, and
register a COM add-in. They will also be able to use one of the shared Office
object models, Command Bars, to programmatically create and modify toolbars
and menus. Students will also be able to create a custom property page for
Outlook 2000 by using a COM add-in.
Materials and Preparation
This section provides you with the materials and preparation needed to teach
this module.
Materials
architecture found in previous versions of Office, and when students should
consider writing code in a COM add-in. Compare COM add-ins and COM
.exe (executable) projects.
Developing a COM Add-in
Explain the purpose of the Add-in Designer and how it is used in a
Microsoft Visual Basic
®
for Applications project. Describe the property
settings in the Add-in Designer. Show how students can add code to the
class file for the designer.
Describe each of the five events supported by the IDTExtensibility2 library
and explain the type of code typically written to handle these events.
Working with a COM Add-in Project
Show the steps required in debugging a COM add-in. Describe the compile
options for COM add-ins. Explain how a COM add-in is registered on a
development computer and on a user’s computer.
Working with Command Bars
Provide an introduction to the CommandBar object, and describe the uses
of its most important properties and methods. Explain how to create, show,
hide, retrieve, and remove command bars.
Describe how to add controls to command bars and set properties for the
controls. Provide a detailed explanation of the process of adding command
bar buttons to the Outlook 2000 Inspector window toolbar from a COM
add-in.
Creating Custom Property Pages for Outlook 2000
Explain what custom property pages are and what they contribute to an
Perform the steps required to debug, compile, and register a COM add-in
within the Visual Basic Editor.
Modify and create command bars, toolbars, and pop-up menus.
Create a custom property page for a Microsoft Outlook
®
2000 folder or the
Outlook 2000 application.
Slide Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
about using COM add-ins
and working with Command
Bars.
2 Module 10: Creating COM Add-ins
Introduction to COM Add-ins
Comparing COM Add-ins and Visual Basic for
Used to automate procedures in Outlook 2000 to
increase a user’s personal productivity
Using a COM Add-in
Self-registering and easily distributed, does not affect
the Outlook 2000 Visual Basic for Applications projects
of users
A common add-in architecture shared across all
Office 2000 applications
How COM Add-ins Can Be Implemented
As a .dll file or .exe fileIn Outlook 2000, you can write Visual Basic for Applications code in either a
COM add-in or a Visual Basic for Applications project.
Using a Visual Basic for Applications Project in
Outlook 2000
A Visual Basic for Applications project is used to automate procedures in
Outlook 2000 to increase a user’s personal productivity. Sharing this project
with other users requires that you import the project file into each Outlook 2000
client where it overwrites all existing Visual Basic for Applications procedures.
Within Outlook 2000, all code is stored in a single Visual Basic for
Applications project—VbaProject.otm.
Other Microsoft Office applications store Visual Basic for Applications
projects differently. Each Microsoft Excel, Microsoft Word, Microsoft Access,
Applications projects of individual users.
In the past, if you wanted to extend Word, Excel, and PowerPoint with the same
solution, you would need to write three separate implementations of the add-in.
All Office 2000 applications now support a common add-in architecture and
development environment. You can write a single COM add-in that will run in
any Office 2000 application. The add-in determines which application has
called it and runs code by using the object model features specific to the
application. The COM add-in can also share common code. For example, place
code that customizes a toolbar within a shared code module that uses the
CommandBar object. Then, the Add-in Designer of each Office application
could invoke the shared code module within the context of the application.
How COM Add-ins Can Be Implemented
A COM add-in can be implemented as a dynamic-link library (.dll) file or as an
executable (.exe) file. Because .dll files generally run more efficiently than .exe
files, this module concentrates on building COM add-ins as .dll files.
COM add-ins replace solutions developed by using Exchange Client
Extensions.
Delivery Tip
Exchange Client Extensions
have to be developed by
using C++ so they are
typically more difficult to
implement than COM add-
ins, which can be developed
in any language that
supports COM add-in
technology. ECE is still an
appropriate solution when
You need to consider a number of issues when deciding whether to develop a
COM add-in. Some of the functionality that COM add-ins provide in
Outlook 2000 is similar to other Microsoft Exchange Server and Outlook 2000
development technologies, such as Microsoft Exchange Server Scripting Agent.
(A scripting agent is a server-side script that runs as a result of events occurring
in folders.)
Event Handling Considerations
Outlook 2000 (and any other host Office application) will fire events that a
COM add-in can implement and handle. These events allow the COM add-in to
gain control over the Outlook 2000 user interface and Outlook 2000 data.
The life span of a COM add-in is controlled by Outlook 2000. When the
Outlook 2000 process is running, the COM add-in can run and receive events.
When Outlook 2000 is not running, the add-in does not run.
If you need to receive events when the Outlook 2000 client is not running, you
should consider using a scripting agent. Because the scripting agent runs on the
server, it will continue to receive events as long as the server is running.
Slide Objective
To list the issues to consider
when deciding to write COM
add-ins.
Lead-in
Writing code in a COM add-
in is not always the best
approach. There are several
issues to consider when
deciding to write a COM
add-in.
6 Module 10: Creating COM Add-ins
You can also add a toolbar button or menu command to view the COM add-ins
available to an Office application. This method is required to view the COM
Add-ins dialog box in Office applications other than Outlook 2000.
Viewing COM Add-ins Available in an Office Application
To view COM add-ins available in an Office application:
1. On the Tools menu, click Customize.
2. Click the Commands tab.
3. In the Categories list box, click Tools.
4. In the Commands list box, click COM Add-ins.
5. Drag the COM Add-ins command to a toolbar or menu, and then click
Close.
Slide Objective
To show the COM Add-ins
dialog box.
Lead-in
To see the COM add-ins
loaded in an Office
application, you must first
add the COM Add-ins
command to a menu or a
toolbar.
8 Module 10: Creating COM Add-ins
Developing a COM Add-in
Visual Basic for Applications productivity tools. These tools include add-ins
such as the Code Librarian, which provides a searchable database that
enable development teams to share a wide range of reusable code easily.
These also include add-ins to automate code documentation, error-handling
routines, and automatic string parsing, as well as other tools for enhancing
productivity.
Learning materials and programming resources. Also included with
Office 2000 Developer are the MSDN
™
Library, hard-copy documentation
and prewritten code for standard routines for Visual Basic for Applications,
and Visual Basic.
Slide Objective
To outline this topic.
Lead-in
You develop COM add-ins
by using an Add-in Designer
to set initial properties of the
add-in and then write code
to handle add-in specific
events.
Module 10: Creating COM Add-ins 9 Creating an Add-in Project
AddInPRoject1 - AddInDesigner1 (AddInDesigner)
General
application you plan to target in the add-in.
The Add-in Designer template is available in Office 2000 Developer and Visual
Basic version 6.0.
Adding a COM Add-in Project to a Visual Basic for Applications Project
To add a COM add-in project to a Visual Basic for Applications project:
1. On the File menu, click New Project.
2. In the New Project dialog box, click Add-in Project, and then click OK.
The Visual Basic Editor adds a COM add-in project to your Visual Basic for
Applications project and displays the Add-in Designer.
Slide Objective
To show the Add-in
Designer project window.
Lead-in
When you add an add-in
project to the Visual Basic
Editor, the Add-in Designer
displays.
10 Module 10: Creating COM Add-ins Working with the Add-in Designer
Setting Add-in Designer Properties
AddIn Display Name
AddIn Description
Application
application. Settings include: None, Startup, Load on
demand, and Load at next startup only.
Slide Objective
To list the properties that
you can set in the Add-in
Designer and to outline
important topics associated
with the Add-in Designer.
Lead-in
The General tab contains
basic information that the
Add-in Designer uses to
register the COM add-in.
Module 10: Creating COM Add-ins 11 Specifying the Initial Load Behavior
When a COM add-in has been properly registered, it will be available to
whatever application is specified in the Add-in Designer that the project
contains. The display name of the registered COM add-in will appear in the
COM Add-ins dialog box.
Selecting the check box next to an add-in in the COM Add-ins dialog box
loads (connects) the add-in and makes it available to the user; clearing the
check box unloads (disconnects) the add-in, so that it cannot be run.
As the developer, you specify when a COM add-in should be loaded. You do
this in the Initial Load Behavior list in the Add-in Designer. You can specify
that an add-in be loaded in one of the following ways:
None. The COM add-in loads only if the user enters the COM Add-ins
Function of the IDTExtensibility2 Library Interface
Provides a programming interface for integrating COM
add-ins with their host applications
Contains preconfigured procedure templates used to
manage COM add-ins in Office
Using Office 2000 Developer or Visual Basic 6.0
Automatically implemented if the Add-in Designer is
used
Using Visual Basic 5.0
Must be manually implementedA COM add-in has events that you can use to run code when the add-in is
loaded or unloaded, or when the host application has finished starting up or
shutting down. To use these events, you must first implement the
IDTExtensibility2 library.
Function of the IDTExtensibility2 Library Interface
The IDTExtensibility2 interface contains events that Office calls when an add-
in is connected to an Office application, whether through the COM Add-ins
dialog box or some other manner. The IDTExtensibility2 interface contains
preconfigured procedure templates (which include their parameter lists) that
you need to manage add-ins in Office.
Using Office 2000 Developer or Visual Basic 6.0
When you create an add-in by using the Add-in Designer template in
3. In the Code window, click IDTExtensibility2 in the Object box. This
creates the OnConnection event procedure stub.
4. To create event procedure stubs for the four remaining event procedures,
click the events in the Procedure box in the Code window.
5. Add code or a comment to each of the five event procedures. You must include the event procedure stub for each event provided by
the IDTExtensibility2 interface. If you omit any of the event procedures, your
project will not compile. If you are not adding code to an event procedure stub,
it is recommended that you add a comment; a single apostrophe (') is sufficient.
Note
14 Module 10: Creating COM Add-ins Handling IDTExtensibility2 Events
OnAddInsUpdate
OnConnection
OnDisconnection
OnStartupComplete
OnBeginShutdownWhen you implement the IDTExtensibility2 library within a class module, the
library makes a set of five events available to the module. These are the events
that you use to control your COM add-in. You will frequently write code for the
OnConnection and OnDisconnection events to manage the functionality of your
COM add-in. While these are methods to the IDTExtensibility2 interface, to a
Visual Basic programmer they act and behave like events. That is, when an add-
exposes five events that you
can handle in your COM
add-in.
Module 10: Creating COM Add-ins 15 You can place code in the OnConnection event that starts the functionality for
your COM add-in. For example, you can create a new menu bar that provides
specific features needed by your Office solution.
The following four parameters pass to the OnConnection event procedure:
Application. This parameter is a reference to the Outlook 2000 Application
object.
ConnectMode. This parameter describes the way in which the COM add-in
was loaded.
AddInInst. This parameter passes an object that refers to the current instance
of your COM add-in.
Custom. This parameter is an array of Variant data types that can hold user-
defined data for your add-in.
The following example displays a message box in response to the
OnConnection event.
Private Sub IDTExtensibility_OnConnection(ByVal _
VBInst As Object, ByVal ConnectMode As _
VBIDE.vbext_ConnectMode, ByVal AddInInst As _
VBIDE.AddIn, custom() As Variant)
MsgBox "Add-in is now connected"
code in the OnDisconnection event that removes the functionality of your COM
add-in. For example, you can remove a menu bar or menu option. Because your
COM add-in will remain available while Outlook 2000 runs, it is important to
remove any functionality created by your COM add-in that will not work in
other documents that the user might create after using your solution.
The following two parameters pass to the OnDisconnection event procedure:
RemoveMode. This parameter specifies how your add-in was disconnected
and can be set to these constants: ext_dm_HostShutdown or
ext_dm_UserClose.
Custom. This parameter is an array of variant data types that can hold user-
defined data for your add-in. (For Office 2000 add-ins, this parameter
should be ignored.)
The following example displays a message box in response to the
OnDisconnection event.
Private Sub IDTExtensibility_OnDisconnection(ByVal _
RemoveMode As VBIDE.vbext_RemoveMode, _
Custom () as Variant)
MsgBox "Add-in is now disconnected"
End Sub If you develop a COM add-in in Visual Basic and do not use the Add-in
Designer (the Add-in Designer is available only in Visual Basic 6.0 and later),
you will need to create a COM .dll that implements the IDTExtensibility2
interface. For more information on the IDTExtensibility2 interface, see the
Microsoft Office 2000/Visual Basic Programmer’s Guide.
perform essentially the same task in each application. The resulting .dll can
contain multiple add-ins that share forms, modules, and class modules, but are
targeted for different applications.
Inserting Additional Add-in Designers into a Project
To insert additional Add-in Designers into a project:
1. On the Insert menu, click Components.
2. In the Components dialog box, click the Designer tab, and then select
Addin Class.
3. In the Project window, right-click the Designers folder for the add-in
project, point to Insert, and then click Addin Class.
4. Set the Application property of the Add-in Designer to the host application
that you want and then set other Add-in Designer properties.
5. Repeat steps 3 through 4 for each additional host application.
Slide Objective
To describe how a COM
add-in can be developed to
run with multiple Office
applications.
Lead-in
You can develop a single
COM add-in that targets
more than one Office
application.
18 Module 10: Creating COM Add-ins Closing a COM Add-in
To Close a COM Add-in
To test for this shutdown problem, on the Outlook 2000 File menu, click Exit
and Log Off. Wait for 20 seconds and then use the Task Manager to see if
Outlook 2000 is still running. If it is still running, either another application is
controlling Outlook 2000—for example Visual Basic or Visual Basic for
Application code in another Office application—or one of the loaded COM
add-ins is not allowing Outlook 2000 close.
Slide Objective
To describe and show how
to close a COM add-in and
Outlook 2000.
Lead-in
You may experience
problems when you close
Outlook 2000 when a COM
add-in is running. These
problems are caused by a
conflict of interests between
Outlook 2000 and the COM
add-In.
Module 10: Creating COM Add-ins 19 Using the Explorers Collection to Solve the Shutdown
Problem
In your Class module, declare an Explorer object by using the WithEvents
keyword.
Public WithEvents objExp As Outlook.Explorer
In the OnConnection event, set the objExp object variable by using the
ActiveExplorer method of the Outlook Application object. Next, add code to
20 Module 10: Creating COM Add-ins
Working with a COM Add-in Project
Debugging a COM Add-in
Compiling a COM Add-in
Registering a COM Add-inOnce you have written the code for a COM add-in, you can use all of the
features of the Visual Basic Editor to debug, compile, and register it.
Slide Objective
To outline this topic.
Lead-in
Once you have written the
code for a COM add-in, you
can use the Visual Basic
Editor to debug, compile,
and register it.
Module 10: Creating COM Add-ins 21 Debugging a COM Add-in