WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE and Outlook
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Delete Mail from Outlook using OLE

Keywords: 	Delete Mail Outlook OLE

Question

: Anyone have any idea how to either move a processed email to the Deleted folder or to delete it right away?

Answer

: Ummm. Unless you are using an IMAP4 mail server and keep your email on the server, then the answer would depend on pretty much what type and version of email program that you use. Some email programs keep each message as a file and are easy to deal with. Other programs have one bug whopping file for each folder. Generally with this sort you have to go thru the programs interfaces. Some programs (Outlook but not OutLookExpress) support OLE that can do a lot of stuff. Here is an example given to us by a user, that might help.....
Here's an Outlook sample that should be adaptable...

objOutlook = ObjectOpen("Outlook.Application")
oln = objOutlook.GetNameSpace("MAPI")
oln.logon(uid, pwd, 0, 1)

;	sent items folder = 5,  deleted items folder = 3, inbox = 6

dfolder = oln.getdefaultfolder(5)
;dfolder = oln.getdefaultfolder(3)
ditems = dfolder.items

ccount = 0

for aa = 1 to 1000

   x = ditems.count

	dmsg = dfolder.items(x)
;	message("Subject", dmsg.subject)

	if timediffdays(rnow, dmsg.senton) > 4
;		message("Sent Date", "should be deleted")
		dmsg.delete
		ccount = ccount + 1
	else
		x = x - 1
	endif
next


oln.logoff
objectclose(objOutlook)

User comments...

Outlook is a bit weird since it updates the folder's contents dynamically, so that if you have 100 items in your folder, you delete one it now knows it has 99. This however moves all the lower messages up one so that you may process an item a few times or that it skips others. I found that even using VB I had the same problem where it would skip emails that it should've taken care of, hence why the loop is set to 1000. I tried decrementing the loop counter after each delete, but that seemed not to produce a remedy. The remedy for this being executing the loop 3 or 5 times to cover any misses.

I had a VB programmer try and he ran into the same problem where it would skip certain messages, while it would deal with others, but he couldn't explain why......

Further Feedback...

If it is desired to process mail, and delete one or more specific mail items, I have found the following partial code to solve the "lost place" problem:

count = 0
count = ditems.count
for aa = 1 to count   
	dmsg = dfolder.items(aa)
;	message("Subject", dmsg.subject)
	if timediffdays(rnow, dmsg.senton) > 4
;		message("Sent Date", "should be deleted")
		dmsg.delete
		aa = aa - 1
		count = count - 1
	endif
next


Article ID:   W14681
Filename:   Delete Mail from Outlook using OLE.txt
File Created: 2005:01:27:15:19:20
Last Updated: 2005:01:27:15:19:20