Search code examples
emailoutlookdelphi-7oleole-automation

Delphi Read Email with Outlook Automation


I have a running code which I can use to read the email body text .The problem is I would like to move the mails that I read. Forexample I read the mail in inbox and I want to move it to another folder in Outlook. After searchin I can do that with Move command but my program gives error "Array out of bounds ". For example I have 4 emails in Outlook inbox it only moves 2 files and gives this error. What is wrong in my code? Please help Thanks

try
ovOutlook := CreateOleObject('Outlook.Application');
ovNameSpace := ovOutlook.GetNameSpace('MAPI');
ovNameSpace.Logon('', '', False, True);
ovFolder := ovNameSpace.GetDefaultFolder(olFolderInbox);
OtherFolder:=ovFolder.Parent.Folders('BTM');
deger:= ovFolder.items.count;
for ii := 1 to deger do begin
 if VarIsNull(ovFolder.Items[ii]) or VarIsEmpty(ovFolder.Items[ii])  then Continue;
  ovEmailItem := ovNameSpace.GetItemFromID(ovFolder.Items[ii].EntryID);
  ovFolder.Items[ii].Move(otherfolder);
end;
finally
ovEmailItem := Unassigned;
ovOutlook := Unassigned;
ovNameSpace := Unassigned;
ovFolder := Unassigned;
OtherFolder :=   Unassigned;
end;

Solution

  • Your code is decreasing the count of messages in the. Loop from Count down to 1:

    for ii := deger downto 1 do begin
    

    I am not sure why you reopen the message inside the loop. There is absolutely no reason to do that. It is also a good idea to avoid multiple dot notation in your code.