How to handle the Open event for Items in Com ADD-in in VC++/ATL?

  • Thread starter Thread starter DarkJem
  • Start date Start date
D

DarkJem

Hi,

I'm currently trying to modify the treatment executed when a user
opens a mail in Outlook (perform security check, get attachments
informations...).
I'm working in C++/ATL to build a COM Add-In.

I'm trying to handle the Open Event on my MailItem but I can't get it
work.
I use the following code :

On top of my addin.h I have :
extern _ATL_FUNC_INFO OnSimpleEventInfo;

I declared :
public IDispEventSimpleImpl<3,CAddin, &__uuidof(Outlook::ItemEvents)>,

protected void __stdcall OpenHandler();

I added this entry in my SINK MAP:
SINK_ENTRY_INFO(3,__uuidof(Outlook::ItemEvents),
/*dispinterface of open
event*/0xf003,OpenHandler,&OnSimpleEventInfo)

typedef IDispEventSimpleImpl</*nID =*/ 3,CAddin,
&__uuidof(Outlook::ItemEvents)> ItEvents;



Then I advise the event in the OnConnection method

ItEvents::DispEventAdvise((IDispatch*)spItem,&__uuidof(Outlook::ItemEvents));

where spItem is declared as CComPtr<Outlook::_Items>

and unadvise it in the ondisconnection :
ItEvents::DispEventUnadvise((IDispatch*)spItem);



And then in addin.cpp I have :

_ATL_FUNC_INFO OnSimpleEventInfo = {CC_STDCALL,VT_EMPTY,0};

void __stdcall CAddin::OpenHandler()
{
MessageBoxW(NULL,L"Opening mail",L"Open Window",MB_OK);
}



There it is. And I get absolutely no message box when I open a mail or
any kind of stuff in Outlook. I don't know why this does not work.
My spItem object is not initialized (I don't know how to initialize it
:-|).
Is it a possible cause?

I did the same with the FolderSwitch Event and it works, I get my
message box each time I change of folder.

Any help or information would be very welcome.

Thanks

Best Regards

Jeremie
 
What do you want to achieve exactly?

It looks like you sink the wrong item (sink Outlook items) ...

Have you tried to sink the Explorer.SelectionChange or
Inspectors.NewInspector instead to get the Outlook item?

Jenny
 
I finally achieve to handle the NewInspector event to detect when a
item is opened. But I get another problem: I put the following code in
the NewInspector event handler:

void __stdcall CAddin::OnInspectorActivate(IDispatch * inspector)
{
MessageBoxW(GetForegroundWindow(),L"InspectorActivate",L"Chgmt
Rep",MB_OK);

// Retrieving current inspector
CComPtr<Outlook::_Inspector> spInspector;
HRESULT hr = m_spApp->ActiveInspector(&spInspector);
if ( FAILED(hr) ) return;
else MessageBoxW(GetForegroundWindow(),L"Inspector
OK",L"Inspector",MB_OK);

// Retrieving the item opened in the Inspector
CComPtr<Outlook::_MailItem> spItem;

hr = spInspector->get_CurrentItem((IDispatch**)&spItem);
if ( FAILED( hr ) ) MessageBoxW(NULL,L"Error retrieving
item",L"Item",MB_OK);
else
{
MessageBoxW(NULL,L"Item OK",L"Item",MB_OK);
BSTR subject;
hr = spItem->get_Subject(&subject);
if ( FAILED( hr ) ) MessageBoxW(NULL,L"Error retieving
sucject",L"Chgmt Rep",MB_OK);
else MessageBoxW(NULL,subject,L"Item",MB_OK);
}
}


It compiles OK and at run Time I get an error when executing hr =
spInspector->get_CurrentItem((IDispatch**)&spItem);

Error :

Debug Assertion failed!
in file atlcomcli.h line 148
Expression p!=0

This error occur only when the first inspector is opened, if I open
another inspector, the error message doesn't appear and the statement
"else MessageBoxW(NULL,subject,L"Item",MB_OK);"
displays the subject of the mail item associated with the Inspector
just as expected.

Have you any idea of how I can get rid of this error on the first
execution of my handler function?

Thanks
Regards

Jérémie
 
Sorry I don't check newsgroups very often ... :)

I would do something like the following instead:

....

CComQIPtr<Outlook::_MailItem> spMailItem;
CComPtr<IDispatch> spCurrItem;

....

if (spInspector == NULL) return E_FAIL;
spInspector->get_CurrentItem(&spCurrItem);
if (spCurrItem == NULL) return E_FAIL;
spMailItem = spCurrItem;

// This is not a mail item
if (spMailItem == NULL) return S_OK;

....


Hope this helps, :)
Jenny
 
Back
Top