Delphi Read Email with Outlook Automation RRS feed

  • Question

  • 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

    ovOutlook := CreateOleObject('Outlook.Application');
    ovNameSpace := ovOutlook.GetNameSpace('MAPI');
    ovNameSpace.Logon('', '', False, True);
    ovFolder := ovNameSpace.GetDefaultFolder(olFolderInbox);
    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);
    ovEmailItem := Unassigned;
    ovOutlook := Unassigned;
    ovNameSpace := Unassigned;
    ovFolder := Unassigned;
    OtherFolder :=   Unassigned;

    R &D Specialist

    Saturday, June 28, 2014 12:47 PM


  • The number of items in the folder is decreased each time you call the Move method. You need to replace the for loop with the while or do/until loop and check out the number of items left in the folder.

    For example, after the first iteration only 3 items are left. Then only two items are left. And the third time the indexer is 3, but the number of items left in the folder is 2.

    Saturday, June 28, 2014 1:04 PM