Commandbar Button Click Handler

  • Thread starter Thread starter saturn
  • Start date Start date
S

saturn

Hi,

I am creating Outlook plug-in using ATL. I have to add a commandbar
(with a button) to only new mail items page. And on the click of that
button I have to execute certain set of code.

For this I have Advised for Outlook::InspectorsEvents on OnConnection.
When the corresponding function is called with new inspector, I checks
if the current item is new mail item.

If item is new mail item then I adds a commandbar and button to the
inspector and sets a bool variable so that I don't need to create it
again in the current session and I removes the commandbar on call to
OnDisconnect. At the same time, i Advise for click of this button
using Office::_CommandBarButtonEvents.

Now my commandbar and is visible whenever I open any new mail item.

All the thinks works fine if there is only one such open mail item and
any click on my button calls the corresponding function.

But if two or more such windows are open simultaneously then none
except the first opened inspector button clicks calls the correponding
function.

Have anyone else have faced any similar problem? What I am doing wrong
here? Any pointers on this will be of great help.

Regards,
Pradeep
 
I don't do C++ so I can't be specific on this for you, but you need to
handle 2 things to get this to work correctly for multiple open Inspectors.

First, create a class that is used to wrap each open Inspector and handle
events for it. Add an instance of the class to a collection or list of some
sort to keep it alive and remove it from the collection when Inspector.Close
fires. Each button you add is then added only to that Inspector. Create the
button using the Temporary = true argument setting.

Second, create a unique Tag property for each button as it's created. That
prevents a click from firing in every open Inspector or firing once for each
open Inspector, the click will then only fire in the correct Inspector and
no other.

I usually create my Tags using a GUID string plus a key value that's
incremented each time a new Inspector is opened. That guarantees a unique
Tag.

As a belt and suspenders approach you also should be explicitly deleting the
button in Inspector.Close so it doesn't persist in the outcmd.dat file if by
some chance Outlook crashes or doesn't shut down correctly to allow the
Temporary setting to take effect.
 
Back
Top