第二个问题牵涉到Outlook中的事件处理。你打开VBE编辑器,在Project列表中展开Microsoft Outlook Objects,双击其下的ThisOutlookSession,然后在代码区上方的左侧列表中找到Application对象,在右侧列表中点击NewMail事件,VBE会自动加入对该事件的处理框架。你就可以在其中编写对该事件(当有新邮件时触发)的处理代码了。VBA代码搞懂后,再做的只是把它转换成Object Pascal了。
procedure TfrmMain.SpeedButton1Click(Sender: TObject);
var
outlook,MailItem,Recipient:variant;
OutlookNameSpace: variant;
OutlookFolder: variant;
OutlookAttachment: variant;
i:integer;
itm: TListItem;
begin
if Checkoutlookexists then
begin
lvHeaders.clear;
try
outlook:=CreateOleObject('OutLook.application');
except
Outlook :=GetActiveOleObject('Outlook.Application');
end;
OutlookNameSpace :=Outlook.GetNamespace('MAPI');
OutlookFolder:=OutlookNameSpace.GetDefaultFolder(olFolderinbox);
MailItem:=OutlookFolder.items;
if MailItem.count>0 then
for i:=1 to MailItem.count do
begin
itm := lvHeaders.Items.Add;
itm.ImageIndex := 5;
itm.Caption := MailItem.item[i].subject;
itm.SubItems.Add(MailItem.item[i].SentOnBehalfOfName);
itm.SubItems.Add(DateToStr( MailItem.item[i].ReceivedTime));
itm.SubItems.Add(IntToStr(MailItem.item[i].Size));
itm.SubItems.Add('n/a');
end;
end;
end;