Solved

Using a For/Next loop to scan email inbox, every other record is skipped if the delete option is set

Posted on 2007-12-05
3
354 Views
Last Modified: 2013-12-05
First of all, apologies for the cryptic title! I couldn't really think of a way to put it concisely!

So, I have an Access ADP project (which many of you will feel you know intimately now!!) and one of its functions is to scan an email inbox looking for email from certain recipients, it then calls a stored procedure that inserts the data it finds into the SQL 2000 database.

This procedure works very well but I have the option on the form that calls the routine to delete the email when it has been imported into the database. When this is checked the routine only scans 50% of the emails (see the code snippet to see the line that does this) so I can kind of see what it is doing (when it looks for "next itm" the last one has been deleted so it gets confused I guess) but haven't a clue how to resolve it.

The function that does it is very long but the relevant bits (I guess) are: (I have omitted declarations etc)

Has anyone else come across a similar problem or any suggestions as to the solution?

Thanks.
Set appOutlook = GetObject(, "Outlook.Application")

Set nms = appOutlook.GetNamespace("MAPI")

 

If SelectFolder = True Then

    Set fld = nms.PickFolder

end if

 

lngItemCount = fld.Items.Count

 

For Each itm In fld.Items

    If itm.Class = 43 Then '43 is the constant olMail

            Set Safeitm = CreateObject("Redemption.SafeMailItem")

            Safeitm.Item = itm

                For Each att In Safeitm.Attachments

                    att.SaveAsFile strFilename

                

                    Set stm = New ADODB.Stream

                    With stm

                        .Type = adTypeBinary

                        .Open

                        .LoadFromFile strFilename

                    End With

                Next att

 

'THE BIT THAT OPENS THE WORD ATTACHMENT, EXTRACTS THE DATA AND CALLS THE STORED PROCEDURE

 

    end if

If Me.chkDeleteEmail True Then Safeitm.Delete

Set Safeitm = Nothing

next itm

Open in new window

0
Comment
Question by:pauldonson
3 Comments
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
I ran into something similar when dealing with deleting from recordsets in the past.

Essentially the pointer gets "lost" when you delete the current record.

Maybe if you do something like build a recordset of the ones you imported and then go back and do a delete as a separate loop.

That was the way I ended up solving the problem.

0
 
LVL 6

Accepted Solution

by:
CRJ2000 earned 500 total points
Comment Utility
You need to loop through the items backwards...

lngItemCount = fld.Items.Count
 
For count = lngItemCount to 1 step -1
  set itm = fld.items(Count)
  ... add your code here...
next

Another option, if you are going to delete all of the items in the folder, then you could simply look at the first item each time... this would only work properly if you are going to delete every item.

For count = 1 to lngItemCount
  set itm = fld.items(1)
  ... add your code here...
  ...Delete The Item Here...
next
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now