Catching buttons pressing in Outlook appointment window.

  • Thread starter Thread starter Sergeichik
  • Start date Start date
S

Sergeichik

I have an Outlook 2007 installed on.
I have small Outlook Add-in example (in C++) somewhere from
http://www.codeproject.com ..

I try to catch buttons, located at 'Appointment window toolbar' (in Outlook
Calendar press some date, and this window will be appear) press by the
following way:

Function OnInspectorEvent is called when new inspector being created:

CAdding private member
CTimeZonesButtonCatcher m_watch

void __stdcall CAddin::OnInspectorEvent (LPDISPATCH ctrl)
{
OutlookInspectorPtr insp(ctrl);
OutlookCommandBarsPtr bars;
HRESULT hr = insp->get_CommandBars(&bars);
m_watch.FindTimeZonesButton(bars);
m_watch.SubscribeToItemEvents();
MessageBox(NULL, "Inspector event hooked !!", "!", MB_OK | MB_APPLMODAL);
}

There is CTimeZonesButtonCatcher class:
header:
//////////////----------------------------------------------
-------------------------------///////////////////////////// /
// TimeZonesButtonCatcher.h

#pragma once

class CTimeZonesButtonCatcher :
public IDispEventSimpleImpl<4, CTimeZonesButtonCatcher,
&__uuidof(Office::_CommandBarButtonEvents)>
{
public:
CTimeZonesButtonCatcher()
{
}

// event sinks map
BEGIN_SINK_MAP(CTimeZonesButtonCatcher)
SINK_ENTRY_INFO(4, __uuidof(Office::_CommandBarButtonEvents), 0x00000001,
OnClick, &CancelableDispEventHandler)
END_SINK_MAP()

public:
// Handles item events
HRESULT(SubscribeToItemEvents)();
HRESULT(UnSubscribeToItemEvents)();
bool FindTimeZonesButton(OutlookCommandBarsPtr& bars);

protected:

virtual void __stdcall OnClick(LPDISPATCH ctrl, VARIANT_BOOL * cancel);

private:

template <typename CONTAINTER_CLASS>
OutlookCommandBarControlPtr FindControl(CONTAINTER_CLASS parent, CComBSTR
caption);

OutlookCommandBarControlPtr TimeZoneButton_m;
};

realization:
//////////////----------------------------------------------
-------------------------------///////////////////////////// /
// TimeZonesButtonCatcher.cpp : implementation of CTimeZonesButtonCatcher

#include "stdafx.h"
#include "TimeZonesButtonCatcher.h"


HRESULT CTimeZonesButtonCatcher::SubscribeToItemEvents()
{
HRESULT hr = IDispEventSimpleImpl<4, CTimeZonesButtonCatcher,
&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise(TimeZoneButton_m, &__uuidof(Office::_CommandBarButtonEvents));
return hr;
}

HRESULT CTimeZonesButtonCatcher::UnSubscribeToItemEvents()
{
HRESULT hr = IDispEventSimpleImpl<4, CTimeZonesButtonCatcher,
&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise(TimeZoneButton_m);
return hr;
}

bool CTimeZonesButtonCatcher::FindTimeZonesButton(OutlookCommandB arsPtr&
bars)
{
if (!bars.p) return false;

// Look for menu bar
int count;
bars->get_Count(&count);

for (int i=1; i<=count; i++)
{
VARIANT index;
index.lVal = i;
index.vt = VT_I4;
OutlookCommandBarPtr bar;
HRESULT hr = bars->get_Item(index, &bar);
CComBSTR barName;
bar->get_Name(&barName);
if(barName == _T("Standard") )
{
OutlookCommandBarControlPtr item = FindControl(bar, _T("I&nvite Attendees") );
if(item.p == NULL)
{
return S_FALSE;
}
TimeZoneButton_m = item;
break;
}
}

if (!TimeZoneButton_m) return S_FALSE;

}


template <typename CONTAINTER_CLASS>
OutlookCommandBarControlPtr
CTimeZonesButtonCatcher::FindControl(CONTAINTER_CLASS parent, CComBSTR
caption)
{
OutlookCommandBarControlsPtr controls;
HRESULT hr = parent->get_Controls(&controls);
int controlsCount;
hr = controls->get_Count(&controlsCount);
for(int i=1; i<=controlsCount; i++)
{
VARIANT controlsIndex;
controlsIndex.lVal = i;
controlsIndex.vt = VT_I4;
OutlookCommandBarControlPtr control;
hr = controls->get_Item(controlsIndex, &control);
CComBSTR controlName;
hr = control->get_Caption(&controlName);
if(controlName == caption)
return control;
}
return OutlookCommandBarControlPtr();
}

void __stdcall CTimeZonesButtonCatcher::OnClick(LPDISPATCH ctrl,
VARIANT_BOOL * cancel)
{
OutlookCommandBarButtonPtr button(ctrl);
MessageBox(NULL, "Hi2222", "Catched !", MB_OK | MB_APPLMODAL);
}
//////////////----------------------------------------------
-------------------------------///////////////////////////// /

line in stdafx.cpp:
_ATL_FUNC_INFO CancelableDispEventHandler = {CC_STDCALL, VT_EMPTY, 2,
{VT_DISPATCH, VT_BYREF | VT_BOOL}};

and lines in stdafx.h:
typedef CComQIPtr<Office::_CommandBars> OutlookCommandBarsPtr;
typedef CComQIPtr<Office::CommandBar> OutlookCommandBarPtr;
typedef CComQIPtr<Office::CommandBarControls> OutlookCommandBarControlsPtr;
typedef CComQIPtr<Office::CommandBarControl> OutlookCommandBarControlPtr;
typedef CComQIPtr<Office::_CommandBarButton> OutlookCommandBarButtonPtr;

extern _ATL_FUNC_INFO DispParamEventHandler;
//////////////----------------------------------------------
-------------------------------///////////////////////////// /


And I'm don't catch button click by such a way, although I catch button
click (using this code) if deal with buttons in main outlook menu, not in
rarely appeared "Appointment window".

How catch in "Appointment" and other windows, appears not just after Outlook
startup ?

Best regards, N.
 
Inspectors in Outlook 2007 use the Ribbon, not the CommandBar interface. You
must use Ribbon callbacks to do what you want. You use XML to set up a
custom Ribbon or to handle existing Ribbon controls and then the callbacks
are called as appropriate.

You should look at some of the sample addins for Outlook 2007 and Office
2007 available for download on the Office Developer Web site at MS. They're
going to be in VB.NET or C# for the most part, but they should give you the
idea.
 
Back
Top