Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For/Next loop missing one out

Status
Not open for further replies.

philcon

Technical User
Feb 5, 2002
139
GB
Hi All
The following piece of code is SUPPOSED to process (detach attachments) each message in an outlook folder, and then move the message to an alternative folder. Almost works fine, but each time I run it, it leaves one message unprocessed. This seems to be the last bug in quite a large project for me, so a foaming pint of lager to whoever can help.[medal]



Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oMessage As Object
Dim sPathName As String
Dim oAttachment As Outlook.Attachment
Dim iCtr As Integer
Dim iAttachCnt As Integer
Dim strSubject As String

On Error GoTo ErrHandler
sPathName = "c:\phil\"

Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox).Folders("briefs")

For Each oMessage In oFldr.Items
If oMessage.Subject = "DTGBBF New Brief" Then
With oMessage.Attachments
iAttachCnt = .Count
If iAttachCnt > 0 Then
For iCtr = 1 To iAttachCnt
.Item(iCtr).SaveAsFile sPathName _
& .Item(iCtr).FileName
Next iCtr
End If
End With
oMessage.Move oFldr.Folders("processed")
End If
DoEvents
Next oMessage
 
Hey,
Don't know a whole lot about outlook but is it possible that it is 0 based? You may want to look at you statement in
Code:
For Each oMessage in oFldr.Items
if for some reason you have a 1 based database it may be missing the first or 0(zero) message or it could be a zero base problem where it is missing the last one. If it is the prior then
Code:
For Each oMessage in oFldr.Items - 1
ought to solve the problem else try
Code:
For Each oMessage in oFldr.Items + 1
to get the last message.

jmtc
Scoty ::) "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Scoty,

Hmmmmmmmmmmmmmmmmmm,,,, , , ,

Interrrrrrrrrrrrstttttttting.

That is the FFIRST quote from old Hymie I have run across in MANY MANY years! Did you run across an (un-authourized?) bio?




MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Seem to get a syntax error trying to do that, the For Each bit is supposed to remove the need to set up a counter.

Phil
 
Mike,
Got it off a quote of the day site. Can't remember which one
::)

Phil,
true the for each does away with the counters but if you trying to compare a zero base and a one base you will need to add or subtract 1 to get to the correct base. are you getting an "out of range" error? or does the
Code:
oFldr.Items
not want to cooperate with the sgl?

Let me know
Scoty
::) "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Scoty,

I think the error relates to the ofldr.items not liking +1 at the end of it.

Some further testing showed that it didn't just miss out the last one. If there were greater numbers of emails in the folder it would leave just under half of them un-processed (I originally tested with just three emails).

Have managed a workaround by putting the whole shabang within a for next loop,checking if there are any e-mails left each time.

Not pretty but it works. Wouldn't mind knowing why the above code didn't work though. Its a puzzle.

Regards


Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top