我希望在Outlook关闭时提示用户。我已经在申请时使用兑换。我不想使用随Delphi提供的TOutlookApplication Class。
请帮助我在Delphi上实现Outlook Onclose / OnQuit事件。
当我使用TOutlookApplication的对象进行OnQuit事件时,如果我的应用程序忙于例如:执行一个超过1分钟的SQL staement,我的outlook会挂起。最终我需要避免这种情况。
请帮助我。
感谢和问候, Vijesh Nair
答案 0 :(得分:3)
前段时间我实现了Outlook事件监听器。我使用导入的Outlook_tlb库来处理outlook。您可以通过IConnectionPoint
界面接收Outlook通知。您的事件侦听器类必须实现IDispatch
接口(至少Invoke
方法)。
所以,有示例代码:
将TOutlookEventListener声明为:
TOutlookEventListener = class(TInterfacedObject, IDispatch)
strict private
FConnectionPoint : IConnectionPoint;
FCookie : integer;
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
public
constructor Create();
end;
在构造函数代码中,您必须获取OutlookApplication的实例,找到连接点并将自身注册为事件侦听器:
constructor TOutlookEventListener.Create();
var cpc : IConnectionPointContainer;
ol : IDispatch;
begin
inherited Create();
ol := GetActiveOleObject('Outlook.Application');
cpc := ol as IConnectionPointContainer;
cpc.FindConnectionPoint(DIID_ApplicationEvents, FConnectionPoint);
FConnectionPoint.Advise(self, FCookie);
end;
使用Invoke
方法可以过滤事件。 Quit
事件的DispID = 61477
function TOutlookEventListener.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
ArgErr: Pointer): HResult;
begin
result := S_OK;
case DispId of
61442 : ; // ItemSend(const Item: IDispatch; var Cancel: WordBool);
61443 : ; // newMailEventAction();
61444 : ; // Reminder(const Item: IDispatch);
61445 : ; // OptionsPagesAdd(const Pages: PropertyPages);
61446 : ; // Startup;
61447 : begin
FConnectionPoint.Unadvise(FCookie);
FConnectionPoint := nil;
form1.OutlookClosed(self);
end
else
result := E_INVALIDARG;
end;
end;
其他方法必须返回E_NOTIMPL结果。
在OnCreate事件处理程序中创建一个TOutlookEventListener实例(假设outlook已经在运行)。我还使用了TForm1.OutlookClosed(sender:TObject)事件来显示通知消息。
阅读有关展望事件的文章:http://www.codeproject.com/Articles/4230/Implementing-Outlook-2002-XP-Event-Sinks-in-MFC-C