Get itemtype of selected item in Outlook

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

Get itemtype of selected item in Outlook
In archive event use only mailitem  
JWBokx




Posts: 12
Joined: 2021-01-12
Hello all,

I hope I can describe my problem so you can undestand it...

I have 2 events.
In ItemLoad event I check if item is a mailitem with QueryInterface.
Then a mailItem of type TMailItem is connected to the item in item load event.

The other event is TadxRibbonCommand atached to Archive archiveAction.
In the event I want an alternative way of archiveing.
If the selected item is a normal mail, it works fine.
But if the mail is an invitation mail, it stops with an error.

This is , I suppose, is caused because the mailItem is not connected to an email item.
But I did not find a way to test if the mailItem is connected?

If I test for example assigned of MAPIObject, it throws a class not registered.

Kind regards,

Jan
Posted 18 Oct, 2021 10:21:11 Top
Andrei Smolin


Add-in Express team


Posts: 18429
Joined: 2006-05-11
Hello Jan,

I'm not sure that I understand you.

JWBokx writes:
Then a mailItem of type TMailItem is connected to the item in item load event.


In what event? ItemLoad?

JWBokx writes:
But if the mail is an invitation mail, it stops with an error.


What error do you get? Are there any details?

JWBokx writes:
If I test for example assigned of MAPIObject, it throws a class not registered.


How do you do this? Show me some code and provide the error message.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 18 Oct, 2021 11:03:43 Top
JWBokx




Posts: 12
Joined: 2021-01-12
Hello Andrei,

I will try to explain bether...

I have an event in my IMPLementation unit of my project:

procedure TAddInModule.adxOutlookAppEvents1ItemLoad(ASender: TObject; const Item: IDispatch);
var
  MItem: outlook2000._MailItem;
begin
  FMailItem.Disconnect;
  Item.QueryInterface(IID__MailItem, MItem);
  if assigned( MItem ) then
  begin
    try
      FMailItem.ConnectTo( MItem );
    finally
      MItem := nil;
    end;
  end;
end;


The FMailIem is a TMailItem.
So if the item in the parameter implements IID_MailItem, the item is connected to the FMailItem.


A second event is an override of the standard archive of Outlook:

procedure TAddInModule.adxRC_ArchiveAction(Sender: TObject;
  const Pressed: Boolean; var Cancel: Boolean);
begin
  Cancel := True; // oude archivering uitschakelen
  if FMailItem.Class_ = olMail then
    if (FMailItem.ConnectKind <> ckAttachToInterface) then
    begin
      TfrmArchiveForm.ShowForm( Self, FMailItem, True );
      UpdateCategory;
    end
//    else
//      Log( 'TAddInModule.adxRC_ArchiveAction: No selected mail found', etInfo );
end;


The problem arrises when the selected item in Outlook is not an emailiten.
Then the FMailItem is not connected to an emailitem.
Using the MAPIObject property throws an exception "Class not registered".

(Right now I cannot place an image of this, I don't have a url to place it quickly)

So in the archive action override I try to find out if the FMailItem is connected.
But the properties like Class_ I found in the forum to check the type throws the exception.
Also checking assigned of the MAPIObject property trows the exception.
Now I can introduce a boolean to be set in the ItemLoad event if an email item is connected, and check this. But I hoped there is a way to test the FMailItem to be connected.

I hope this is more understandable....

Kind regards,

Jan
Posted 19 Oct, 2021 02:19:51 Top
Andrei Smolin


Add-in Express team


Posts: 18429
Joined: 2006-05-11
Hello Jan,

In your adxOutlookAppEvents1ItemLoad you leave FMailItem assigned. If you are interested in mail items only, you should set FMailItem := nil for non-mail items. This would let you check whether it is nil in your adxRC_ArchiveAction procedure.

Another approach would be to use OLEVariant without QueryInterface:

var
OutlookItem: OLEVariant;
...
OutlookItem := Item;
...
if OutlookItem.Class_ = olMail then
...

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 19 Oct, 2021 05:26:45 Top
JWBokx




Posts: 12
Joined: 2021-01-12
Hello Andrei,

Aah yes ofcourse...
Sometimes looking much to diffecult...

Thankyou,

Jan
Posted 19 Oct, 2021 07:31:41 Top
Andrei Smolin


Add-in Express team


Posts: 18429
Joined: 2006-05-11
No problem!

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 19 Oct, 2021 09:01:01 Top