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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incorrect reading number of emails in Outlook folder... 2

Status
Not open for further replies.

franksirvent

Programmer
Mar 8, 2002
358
GB
Hi there
I have the following code (part of) which goes through a folder in Outlook and saves the attachments to C:\attachments
It all works well, however if there are 3 emails in the folder, it only reads the first 2.
So it seems to leave 1 email till the next time code is run...

Is there a way of processing all emails without leaving any behind ????

thanks in advance

Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set oFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set oExtractFolder = oFolder.Folders("EXTRACTS")


For Each Item In oExtractFolder.Items
strControl = strControl + 1
If InStr(Item.Subject, "ESVIT02") Then
Item.Attachments.Item(1).SaveAsFile "C:\ATTACH\XXX.ZIP"
Item.Delete


Else
End If

Next Item
 
Are you perhaps getting into trouble because your collection variable, Item, is a "reserved" word in VBA so Item.Attachments.Item(1) might be screwing with the value of Item?

_________________
Bob Rashkin
 
you may try something like this:
For i = oExtractFolder.Items.Count To 1 Step -1
Set oItem = oExtractFolder.Items(i)
strControl = strControl + 1
If InStr(oItem.Subject, "ESVIT02") Then
oItem.Attachments.Item(1).SaveAsFile "C:\ATTACH\XXX.ZIP"
oItem.Delete
End If
Set oItem = Nothing
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hi there
thanks for helping me

my knowledge with VBA is minimum so I was not aware of the Item thing being areserved word.

I have done what PHV advised, (which shows Item as oItem) and it works perfecly so I assume that was the problem

Thank you both!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top