Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Outlook Automation Scaning Outlooks Folders and reading Mail

Title: Outlook Automation - Scaning Outlook's Folders and reading Mail Question: How to serf in Outlook from myself application and read Mail Answer: unit UScanOutlook; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, Outline; const olByValue = 1; olByReference = 4; olEmbeddedItem = 5; olOLE = 6; olMailItem = 0; olAppointmentItem = 1; olContactItem = 2; olTaskItem = 3; olJournalItem = 4; olNoteItem = 5; olPostItem = 6; olFolderDeletedItems = 3; olFolderOutbox = 4; olFolderSentMail = 5; olFolderInbox = 6; olFolderCalendar = 9; olFolderContacts = 10; olFolderJournal = 11; olFolderNotes = 12; olFolderTasks = 13; type TItem = class(TObject) Letter:OleVariant; name:string; end; TForm1 = class(TForm) oline_outlook: TOutline; Button8: TButton; procedure Button8Click(Sender: TObject); procedure oline_outlookDblClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); private { Private declarations } public { Public declarations } OlApp,NameSpace,root:OleVariant; List:Tlist; Item:TItem; icount:integer; end; var Form1: TForm1; implementation uses ComObj; {$R *.DFM} procedure TForm1.Button8Click(Sender: TObject); procedure scan(ol:TOutline;root:OleVariant;s:string); var i,j,k:integer; bcount,rcount:integer; branch,MAPIFolder:olevariant; line:string; begin line:=''; rcount:=root.count; for i:=1 to rcount do begin line:=s+root.item[i].name; ol.Lines.Add(line); List.Add(TItem.Create); Item:=List.items[List.count-1]; Item.name:='Folder'; branch:=root.item[i].folders; bcount:=branch.count; MAPIFolder:=Namespace.GetFolderFromId(root.item[i].EntryID,root.item[i].StoreID); if MAPIFolder.Items.count0 then for j:=1 to MAPIFolder.Items.count do begin ol.Lines.Add(s+' '+MAPIFolder.Items[j].subject); List.Add(TItem.Create); Item:=List.items[List.count-1]; Item.name:='File'; Item.Letter:=MAPIFolder.Items[j]; end; if bcount0 then begin scan(ol,branch,s+' '); end; end; end; begin oline_outlook.Lines.Clear; OlApp:=CreateOleObject('Outlook.Application'); Namespace:=OlApp.GetNameSpace('MAPI'); root:=Namespace.folders; scan(oline_outlook,root,''); end; procedure TForm1.oline_outlookDblClick(Sender: TObject); begin Item:=List.items[oline_outlook.SelectedItem-1]; if Item.name='File' then ShowMessage(Item.Letter.Body); end; procedure TForm1.FormCreate(Sender: TObject); begin List:=TList.Create; Item:=TItem.Create; icount:=0; end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); var i:integer; begin for i:=List.Count-1 downto 0 do begin Item:=List.Items[i]; Item.Free; end; List.Free; end; end.