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: 18
Joined: 2014-11-22
Andrei,
we had this topic already in December 2014: https://www.add-in-express.com/forum/read.php?FID=1&TID=12851

The code we worked out worked most of the time, however I have a customer who complains that my Add-in is crashing Outlook, when the user is moving an unread MailItem from the Inbox to another folder. For already read emails (not in bold subject) there is no problem. (on my testsystem I cannot replicate the error)

Could you advise where the below descibed error might come from?

Thanks,
Ralf


- Outlook Version: OUTLOOK.EXE, Version: 14.0.7165.5000

- Error Code: "Add-in execution error. Outlook crashed during the 'SelectionChange' callback of the 'ExplorerEvents_10' interface while calling into the 'xxxxx_class' add-in." (xxxxx is my add-in)

The code I use is the following, where MItem is a global variable:
MItem: Outlook2000.TMailItem;

procedure TAddInModule.ConnectToFolder();
begin
if not Assigned(FFolderEvents) then begin
FFolderEvents := TMAPIFolderEvents.Create(nil);
FFolderEvents.OnBeforeFolderMove := DoBeforeFolderMove;
FFolderEvents.OnBeforeItemMove := DoBeforeItemMove;
end;

FFolderEvents.ConnectTo(OutlookApp.ActiveExplorer().CurrentFolder);
end;

procedure TAddInModule.DoBeforeFolderMove(ASender: TObject;
const MoveTo: MAPIFolder; var Cancel: WordBool);
begin
if Assigned(MItem) then MItem.Disconnect();
Cancel := False;
// ShowMessage('Before Folder Move!');
end;

procedure TAddInModule.DoBeforeItemMove(ASender: TObject;
const Item: IDispatch; const MoveTo: MAPIFolder; var Cancel: WordBool);
begin
if Assigned(MItem) then MItem.Disconnect();
Cancel := False;
// ShowMessage('Before Item Move!');
end;


procedure TAddInModule.adxOutlookAppEvents1ExplorerSelectionChange(Sender: TObject);
var
sel : Selection;
item: _MailItem;
begin
try
sel := OutlookApp.ActiveExplorer.Selection;
except
end;
if Assigned(sel) and (sel.Count = 1) then begin
sel.Item(1).QueryInterface(IID__MailItem, item);
if Assigned(item) then begin
// showMessage('Selection Change');
if Assigned(MItem) then MItem.Disconnect();
MItem := TMailItem.Create(self);
readXHeaderInfo := GetXHeader(item.MAPIOBJECT as IMessage, myXHeaderName); // this calls a procedure to read X-Header inforamtion from the selected MailItem
MItem.OnReply := DoOnReply;
MItem.OnReplyAll := DoOnReplyAll;
MItem.OnForward := DoOnForward;
MItem.ConnectTo(item);
ConnectToFolder(); // required to disconnect from MItem when item is moved
item := nil;
end;
end;
sel := nil;
end;
Posted 30 May, 2016 16:53:14 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Ralf,

You can add debug messages to the code of the adxOutlookAppEvents1ExplorerSelectionChange procedure. Note however that using ShowMessage() isn't recommended; such messages produce unwanted events in the Outlook object model (Activate/Deactivate). Instead, use OutputDebugString (http://docwiki.embarcadero.com/RADStudio/Seattle/en/OutputDebugString); you collect these messages at run time using DebugView (see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx).

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 31 May, 2016 05:12:38 Top
Ralf Garbe




Posts: 18
Joined: 2014-11-22
Hello Andrei,
Great I will have a study on the debug method shown.
Unfortunatelly I will not be able to do that with my customer.
I assume it is important to know during which command the application fails - I will find a solution where the output will be written to a file.
I will let you know then.
Any other idea how and when such behaviour occurs? Or is there better code to achieve the above?

Thanks for your help,
Ralf
Posted 31 May, 2016 15:49:19 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Ralf,

I have no suggestions. The code looks correct to me.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 01 Jun, 2016 06:04:55 Top
Ralf Garbe




Posts: 18
Joined: 2014-11-22
Hello Andrei,
thanks for your support.
Looks like the problem is in the function to read X-Header information from the Email:
readXHeaderInfo := GetXHeader(item.MAPIOBJECT as IMessage, myXHeaderName);

I will further debug that function and might open an new Topic if necessary.

Thanks again,
Ralf
Posted 01 Jun, 2016 14:22:58 Top
Andrei Smolin


Add-in Express team


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

No problem.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 Jun, 2016 03:11:20 Top