Outlook - OnReply, OnAllReply, OnForward

Add-in Express™ Support Service
That's what is more important than anything else

Outlook - OnReply, OnAllReply, OnForward
how to get the email events of Reply, ReplyAll and Forward 
Ralf Garbe




Posts: 20
Joined: 2014-11-22
Hi,
I need to react on emails when the user replies or forwards an email.
In the past I had found code here, but I have the impression that in some cases (for instance when moving emails to other folders), and only sometimes, Outlook crashes.
The code I used so far was as follows:

procedure TAddInModule.adxOutlookAppEvents1ExplorerSelectionChange(Sender: TObject);
var
sel : Selection;
item: _MailItem;
xClass, xRel: string;
begin
{
try
sel := OutlookApp.ActiveExplorer.Selection;
except
end;
if Assigned(sel) and (sel.Count > 0) then begin
sel.Item(1).QueryInterface(IID__MailItem, item);
if Assigned(item) then begin
if Assigned(MItem) then MItem.Disconnect(); MItem := TMailItem.Create(nil);
MItem.OnReply := DoOnReply;
MItem.OnReplyAll := DoOnReplyAll;
MItem.OnForward := DoOnForward;
MItem.ConnectTo(item);
item := nil;
end;
end;
sel := nil;
}
end;

Hope you can help,
Thanks,
Ralf
Posted 22 Nov, 2014 14:36:11 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Hello Ralph,

I would try to disconnect from that item in the BeforeItemMove event of the Folder object. The Folder object and the BeforeItemMove event were introduced in Outlook 2007. Please see
http://msdn.microsoft.com/en-us/library/office/ff869445%28v=office.15%29.aspx
http://msdn.microsoft.com/en-us/library/office/ff863890%28v=office.15%29.aspx

I also believe you disconnect from that item in the Explorer.Deactivate event.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Nov, 2014 03:51:32 Top
Ralf Garbe




Posts: 20
Joined: 2014-11-22
Hello Andrei,
thanks for the answer, however I'm not sure how to do the suggested.
The adxOutlookAppEvents component does not provide the BeforeItemMove event. I just found the events
- OnExplorerBeforeItemCopy, OnExplorerBeforeItemCut and OnExplorerBeforeItemPaste.

Thanks for your help,
Regards,
Ralf
Posted 26 Nov, 2014 14:39:54 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Ralf,

According to the links above, you need to get a Folder object from the type library of Outlook 2007-2013. You may need to import the corresponding type library; an example of importing and using the type library for Outlook 2010 is given at http://www.add-in-express.com/forum/read.php?FID=1&TID=12731&MID=64876#message64876.

Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Nov, 2014 02:18:39 Top
Ralf Garbe




Posts: 20
Joined: 2014-11-22
Hello Andrei,
thanks for the suggestion, however the suggested creates more problems.
The 2010 TLB does not support TMailItem, but MailItem, which does not support any methods.

Could you consider once more how to react on the Reply, ReplyAll and Forward events to an email.

Thanks,
Ralf
Posted 27 Nov, 2014 15:53:26 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Ralf,

You need to use TMailItem from the Office 2000 TLB. You need to use the Office 2007-2013 TLB to connect to the BeforeItemMove event only.

Do I answer your questions?


Andrei Smolin
Add-in Express Team Leader
Posted 28 Nov, 2014 03:21:42 Top
Ralf Garbe




Posts: 20
Joined: 2014-11-22
Hello Andrei,
I'm not good in COM programming, that's why I use Add-in Express.

I was able to add to the code below, but it does not help me, nor does it provide me the ability to link to the BeforeItemMove event.
Could you provide me an example in the above code which links to the BeforeItemMove event, and show me how the BeforeItemMove event needs to be defined?

Thanks for your help,
Ralf

procedure TAddInModule.adxOutlookAppEvents1ExplorerSelectionChange(Sender: TObject);
var
ol2010: Outlook2010._Application;
aFolder: Outlook2010.Folder;
i: integer;
begin
ol2010:= self.OutlookApp.Application as Outlook2010._Application;
for i := 1 to ol2010.Session.Stores.Count do
showMessage('Store ' + intToStr(i) + ': ' + ol2010.Session.Stores.Item(i).DisplayName);

aFolder := ol2010.Session.Folders.Item(1);
for I := 1 to aFolder.Items.Count do
showMessage('Folder Name: ' + aFolder.Folders.Item(i).Name);

end;
Posted 29 Nov, 2014 17:10:40 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Hello Ralph,

Please have a look at http://temp.add-in-express.com/support/test/OutlookBeforeItemMoveSample.zip. This will work in Outlook 2007+.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Dec, 2014 10:11:25 Top
Ralf Garbe




Posts: 20
Joined: 2014-11-22
Anderei,
many thanks!
Even I could integrate this in my code :)
For future versions of Add-In Exress
You might want to consider adding the events of OnReply, OnAllReply, OnForward in future version of Add-In Express.

Best Regards,
Ralf
Posted 01 Dec, 2014 14:30:25 Top
Andrei Smolin


Add-in Express team


Posts: 18791
Joined: 2006-05-11
Thank you for the suggestion!


Andrei Smolin
Add-in Express Team Leader
Posted 02 Dec, 2014 09:02:03 Top