Board index » delphi » Canceling default action of a button in Outlook.

Canceling default action of a button in Outlook.


2005-10-11 12:30:32 AM
delphi180
I am trying to write an COM Addin for Outlook 2000 using Delphi 7 that
intercepts when a user is trying to add an attachment to an e-mail by
hooking into the OnClick event for that button and setting CancelDefault to
True. This is, I believe, how you are supposed to be able to override
default button behaviour in Outlook.
I have created a tCommandBarButton wrapper object that is connected to the
'Attach' button, and I have written a simple OnClick handler that pops up a
message and sets CancelDefault to True.
However, when I click my button, I not only get my message box, I also get
the 'Insert File' dialog appearing, even though this is supposed to be
cancelled. (See below for code sample)
Therefore, I'd be most grateful if anyone could give me any pointers as
to what I may be doing wrong, or if there is a bug in the Delphi
implementation, etc...
Thanks,
Regards,
Paul Bayston
------------------------------------------------------------
procedure TAttachmentTest2.MyNewInspector( Sender: tOutlookInspectors; const
MyInspector: _Inspector );
var
xButton: CommandBarButton;
xButtonWrapper: TOutlookCommandBarButton;
xStandard: CommandBar;
xInspector: TOutlookInspector;
begin
xInspector := TOutlookInspector.Create( MyInspector );
xInspector.OnClose := MyCloseInspector;
xStandard := MyInspector.CommandBars[ 'Standard' ];
xButton := xStandard.Controls.Item[ 10 ] as CommandBarButton;// Current
index of Attach button
xButtonWrapper := TOutlookCommandBarButton.Create(
xStandard.Controls.Item[ 10 ] );
xButtonWrapper.OnClick := OnButtonClick;
end;
procedure TAttachmentTest2.OnButtonClick( Sender: TOutlookCommandBarButton;
const Ctrl: CommandBarButton; var CancelDefault: WordBool );
begin
ShowMessage( 'Hello' );
CancelDefault := True;
end;
 
 

Re:Canceling default action of a button in Outlook.

Delphi has problems with var parameters in the event handlers (don't wait
for Borland to fix that anytime soon, who needs COM these days?)
Either create your own wrapper or use the wrappers from
www.techvanguards.com/products/eventsinkimp/
Also note that you should never access controls by the index: besides the
fact that teh user can rearrange the buttons, it is very likely to breaks in
other version of Outlook.
Use CommandBars.FindControl() specifying type and id.
Dmitry Streblechenko (MVP)
www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
"Paul Bayston" <XXXX@XXXXX.COM>writes
Quote
I am trying to write an COM Addin for Outlook 2000 using Delphi 7 that
intercepts when a user is trying to add an attachment to an e-mail by
hooking into the OnClick event for that button and setting CancelDefault to
True. This is, I believe, how you are supposed to be able to override
default button behaviour in Outlook.

I have created a tCommandBarButton wrapper object that is connected to the
'Attach' button, and I have written a simple OnClick handler that pops up
a message and sets CancelDefault to True.

However, when I click my button, I not only get my message box, I also get
the 'Insert File' dialog appearing, even though this is supposed to be
cancelled. (See below for code sample)

Therefore, I'd be most grateful if anyone could give me any pointers
as to what I may be doing wrong, or if there is a bug in the Delphi
implementation, etc...

Thanks,

Regards,

Paul Bayston

------------------------------------------------------------

procedure TAttachmentTest2.MyNewInspector( Sender: tOutlookInspectors;
const MyInspector: _Inspector );
var
xButton: CommandBarButton;
xButtonWrapper: TOutlookCommandBarButton;
xStandard: CommandBar;
xInspector: TOutlookInspector;
begin
xInspector := TOutlookInspector.Create( MyInspector );
xInspector.OnClose := MyCloseInspector;

xStandard := MyInspector.CommandBars[ 'Standard' ];

xButton := xStandard.Controls.Item[ 10 ] as CommandBarButton;// Current
index of Attach button

xButtonWrapper := TOutlookCommandBarButton.Create(
xStandard.Controls.Item[ 10 ] );
xButtonWrapper.OnClick := OnButtonClick;
end;

procedure TAttachmentTest2.OnButtonClick( Sender:
TOutlookCommandBarButton; const Ctrl: CommandBarButton; var CancelDefault:
WordBool );
begin
ShowMessage( 'Hello' );
CancelDefault := True;
end;