Inbox folder in Outlook is not restricted to contain only mail messages. When iterating through its items you can encounter various item classes like MailItem
, PostItem
, MeetingItem
, TaskRequestItem
and many others. All of these items support different sets of properties and not all of them have Sender
, SenderEmailAddress
or Attachments
property. If you’re interested only in mail items then you need to check item’s Class
property:
const
olMail = $0000002B;
{ ... }
for i := iNbMail downto 1 do
begin
Mail := Inbox.Items[i];
if Mail.Class <> olMail then
Continue;
{ here we can assume we're working with MailItem instance }
end;
I doubt that Outlook ever returns null or empty item, so the check you do in your code is pointless. If you’re interested in other item classes then check out OlObjectClass
enumeration.
I’m not sure why you prefer to use late-binding, because Delphi already comes with imported type library Outlook2010.pas
to automate Outlook in strongly typed manner. The library is located in OCX\Servers
subfolder of installation folder. In case you need to support pre-2010 Outlook versions you can use unit OutlookXP
or even Outlook2000
instead. The code fore iterating mail items using the type library could look like this:
uses
System.SysUtils, System.Variants, Winapi.ActiveX, Outlook2010;
function GetOutlookApplication: OutlookApplication;
var
ActiveObject: IUnknown;
begin
if Succeeded(GetActiveObject(OutlookApplication, nil, ActiveObject)) then
Result := ActiveObject as OutlookApplication
else
Result := CoOutlookApplication.Create;
end;
procedure ProcessInboxItems;
var
Outlook: OutlookApplication;
Inbox: Folder;
Index: Integer;
LItems: Items;
LMailItem: MailItem;
begin
Outlook := GetOutlookApplication;
Outlook.Session.Logon(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
Inbox := Outlook.Session.GetDefaultFolder(olFolderInbox);
LItems := Inbox.Items;
for Index := LItems.Count downto 1 do
begin
if Supports(LItems.Item(Index), MailItem, LMailItem) then
begin
{ do whatever you wish with LMailItem }
end;
end;
end;
CLICK HERE to find out more related problems solutions.