1. I see that I have to 'Implement IDTExtensibility2' which I assume tells
the Outlook COM architecture that someone is listening to it, and when
certain events occur, that Outlook should call the COM add-in object's
functions (like *_OnDisconnection). Is that correct? I know that function
bodies for 5 functions are required by IDTExtensibility2. I do some
checking to make sure we're alive and well inside Outlook in those
functions via some Debug.Print statements. Is that all those functions are
for?
2. Now I want to go beyond that and add a button to a new mail message
window. At what point (in the 5 calls IDTExtensibility2 requires) are all
Outlook's objects valid? When can I get the Application? The CommandBars?
And the rest? The real question is this, do I have to do all my work
inside those 5 functions? If I can add functionality (like when the user
creates a new mail message), where does it go and what do I name the
methods. It seems that there's a reason behind the naming schemes of the
functions that are there, but I don't see where my stuff would fit in.
Thanks. Comments/criticisms/links/source are all welcome. Sorry for the
basic nature of these questions, but Outlook add-in documentation seems to
be a bit lacking.
Regards,
Paxton
--
Paxton C. Sanders
pcsa...@yahoo.com
"When everything has fallen apart, remember one thing: It could be worse."
> 1. I see that I have to 'Implement IDTExtensibility2' which I assume
tells
> the Outlook COM architecture that someone is listening to it, and when
> certain events occur, that Outlook should call the COM add-in object's
> functions (like *_OnDisconnection). Is that correct?
Well, sort of, but not really. 'Implement IDTExtensibility2' tells the
component itself that it better be "listening" for those events, and tells
those other objects which use this component that they can rely on that
interface. When you implement the interface, you are making a "Contract"
with the component calling yours that everything in the interface will
exist. For example, in my COM Add-Ins, I don't usually do anything on
OnStartupComplete, but I have it in my CAI, because the host application
(Excel, in my case) expect it to be there. In my CAI, the event code is
there, but it just has a single line comment. It doesn't *do* anything, but
it exists.
When you implement an interface, the host doesn't have to know anything
about *your* specific component, only the interface you're implementing.
Therefore, it can communicate with you via a known set of events. You must
accept all of those, even if you don't use them do really do anything.
> 2. Now I want to go beyond that and add a button to a new mail message
> window. At what point (in the 5 calls IDTExtensibility2 requires) are all
> Outlook's objects valid? When can I get the Application? The
CommandBars?
My CAIs are for Excel, not Outlook, but this shouldn't make a difference. I
build my command bar controls in then IDTExtensibility2_OnConnection event.
In the designer's class module, I declare WithEvents an
Office.CommandBarControl object for each button or menu item. E.g.,
Private WithEvents pBtnView As Office.CommandBarButton
In the same module are the _Click events for those controls:
Private Sub pBtnView_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
'
' Your code here
'
End Sub
You have to set the objects to the buttons you Add. E.g.,
Set pBtnView = AddViewButton(Application, ConnectMode, AddInInst)
Where AddViewButton is another procedure you write. It creates the command
buttons in the same manner you do in standard Outlook. E.g.,
Dim CmdCtrl As Office.CommandBarButton
On Error Resume Next
'
' Add new button.
'
Set CmdCtrl = gTopLevelControl.Controls.Add(Type:=msoControlButton, _
Parameter:="", temporary:=True)
With CmdCtrl
.Caption = "RangeWatch"
.Tag = CreateTag("ShowAppWatch")
.Style = msoButtonCaption
.OnAction = PROG_ID_START & AddInInst.ProgId & PROG_ID_END
End With
Set AddViewButton = CmdCtrl
Note that the OnAction property is set to the ProgId of this AddInInst,
*not* the actual procedure name that will be executed. What is actually
executed is the Click Event. Therefore, all your command buttons will have
the same OnAction setting. The PROG_ID_START and PROG_ID_END are just
string constants for "<" and ">". These are required to tell Excel to use
the rest as a ProgID, not an actual macro name.
> When can I get the Application?
You get the application and the add-in instance in the OnConnection event.
These are passed in.
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Save these objects in global variables, so you can use them later. In my
add-in, I have global variables declared as
Public gXL As Excel.Application
Public gAddInInst As Object
and the first thing I do in OnConnection is
Set gHostApp = Application
Set gAddInInst = AddInInst
> do I have to do all my work
> inside those 5 functions? If I can add functionality (like when the user
> creates a new mail message), where does it go and what do I name the
> methods.
No. Those 5 procedures are only called during in Startup and Shutdown of
your CAI. If you don't have code outside these procedures, your CAI will be
able to do things only when it is loaded and unloaded. The rest of the
time, it sits idle. As I said before, code for user-initiated actions are
contained in the Click events for the Command Bar Buttons.
If you want to have code executed in response to the Application itself, you
need to include a class module, with the host application defined
WithEvents. Again, I'm writing for Excel, but the concept is the same of
Outlook.
I have a class called CAppClass, with the following declaration:
Public WithEvents XLEventApp As Excel.Application
Now, this object will receive the standard Excel application events. Of
course, this has to be set to the instance of Excel that we're presently
dealing with, so in the OnConnection event of the Designer, I use
Set AppClass = New CAppClass
Set AppClass.XLEventApp = gXL
This creates a new instance of the CAppClass class, and then sets the
XLEventApp object to the existing Excel application. (Remember, gXL is a
global variable that we declared and set few paragraphs earlier.)
The standard events are then executed in the CAppClass module. For example,
Excel has a SheetChange event procedure, which is automatically called when
a cell on a worksheet is changed. This is declared as
Private Sub XLEventApp_SheetChange(ByVal Sh As Object, ByVal Target As
Excel.Range)
'
' my code here
'
End Sub
> Thanks. Comments/criticisms/links/source are all welcome. Sorry for the
> basic nature of these questions, but Outlook add-in documentation seems to
> be a bit lacking.
I hope this is helpful. I have a fairly nice COM add-in for Excel2000 which
gives you a "watch window" to watch values of any cells in any workbooks,
and an "alert window" to display messages when cells are outside of some
limits (e.g., greater than 1000 or something).
I'd be more than happy to send you the source code (VB6) and the DLL file.
The CAI stuff is quite well documented, but the Excel-specific stuff isn't.
I learned about CAI almost totally by trial and error, so I put alot of
comments as I was learning. Send me an email if you want a copy of the
code.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com ch...@cpearson.com
"Paxton C. Sanders" <pcsa...@yahoo.com> wrote in message
news:efsdbgERAHA.289@cppssbbsa04...
<SNIP>
Thank you very much for the detailed response. I think my missing link was
the WithEvents keyword. It seems to be what opens the framework up to my
code. I'll poke through some of that infrastructure and see what I can dig
up.
Meanwhile, I've printed out your response and I'll digest it slowly.
Again, I appreciate you going above the call of duty with such a detailed
response.
> I think my missing link was
> the WithEvents keyword.
Yeah, it does alot more than what you may think. It is like declaring
Dim MyFerrari As Ferrari
instead of
Dim WithGas MyFerrari As Ferrari
You may have a beautiful Ferrari in your driveway, but if it doesn't have
any gas, it is no fun. All you can do is look at it.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com ch...@cpearson.com
"Paxton C. Sanders" <pcsa...@yahoo.com> wrote in message
news:eR9a17Q...@cppssbbsa02.microsoft.com...
Q230225 - "OL2000: How to Create a COM Add-in for Outlook"
http://support.microsoft.com/support/kb/articles/q230/2/25.asp
--
Chad McCaffery
Microsoft Support / Newsgroup lurker
===================================================
"Paxton C. Sanders" <pcsa...@yahoo.com> wrote in message
news:eR9a17Q...@cppssbbsa02.microsoft.com...