1

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;
DelphiCoding
  • 23
  • 1
  • 6
  • Use the debugger. Set a breakpoint on the `for ii` line, and run to the breakpoint. Now check to see what deger is, and then step through the loop slowly, keeping track of what value `ii` has each time. What is the index when the exception happens? If `deger` is 4, and `ii = 4` when the exception happens, it means that the array is 0-based, and your loop should run from 0 to deger - 1 instead. If that's not the case, you'll at least know which line of code is causing the problem so you'll know where to look trying to solve the problem. – Ken White Jun 28 '14 at 20:15
  • I already did that but still no point. Forexample if there is 4 mails in outlook inbox , the deger is 4 but when it comes to for loop it stops and gave error when deger is3 – DelphiCoding Jun 29 '14 at 20:53

1 Answers1

0

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • my aim is to open the mail that comes to outlook inbox, read the body text , save it to a txt file and then move or delete the mail. Thats why i am trying for loop but it gives array out of bound error:( – DelphiCoding Jun 30 '14 at 06:33