For Each... Next Loop

B

Brad

Thanks for reading my question.

I am just learning how to write code for Outlook and am
having problems with a For Each... Nex loop.

I can't seem to assign my variable the right value, see
comments in code.

Thanks again,

Brad


Public Sub Test2()

Dim i As Variant, MyCollection As Variant

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set MyNewFolder = myFolder.Folders
("EarlyShippingReportsNew")
'myFolder.Display


'I Get Stuck Here
'I can't seem to assign i and MyCollection the correct
values.
'I know the code inside the loop works as I had it all
running before I added the loop
'I want the code to execute on each e-mail in myNewFolder

Set i = MailItem
Set MyCollection = MyNewFolder
For Each i In MyCollection
Set myItem = MyNewFolder.Items(1)
myItem.Display
MsgBox myItem.Subject
MsgBox myItem.Body
MsgBox myItem.Text
'myItem.Close
Set myDestFolder = myFolder.Folders
("EarlyShippingReportsSent")
myItem.Move myDestFolder
Next

End Sub
 
P

Patricia Cardoza - [MVP Outlook]

Every time you enter the loop you're setting MyItem to the first item. So
you're never getting past the first item. Change your look to something like
the following.

Set MyCollection = MyNewFolder.Items
For Each i In MyCollection
Set myItem = i
myItem.Display
MsgBox myItem.Subject
MsgBox myItem.Body
MsgBox myItem.Text
'myItem.Close
Set myDestFolder = myFolder.Folders
("EarlyShippingReportsSent")
myItem.Move myDestFolder
Next

--
Patricia Cardoza
Outlook MVP
www.cardozasolutions.com

Author, Special Edition Using Microsoft Outlook 2003

***Please post all replies to the newsgroups***
 
S

Sue Mosher [MVP]

MyNewFolder is a folder, not a collection. What collection do you want to
work with? Every MAPIFolder has two collections -- Items and Folders.
 
G

Guest

Hi Sue,

Thanks for the help on this one and the last one. :)

I want the code to iterate through each e-mail (i) in the
EarlyShippingReportsNew folder (MyCollection)

Thanks,

Brad
 
S

Sue Mosher [MVP]

Then you need to set MyCollection to the Items collection:

Set MyCollection = MyNewFolder.Items

Because you are moving items, however, you cannot use a For Each loop.
Instead, use a countdown loop:

Set myDestFolder = myFolder.Folders("EarlyShippingReportsSent")
intCount = MyCollection.Count
For i = intCount to 1 Step -1
Set myItem = MyCollection(i)
' your code to do stuff with myItem goes here

myItem.Move myDestFolder
Next

Was there a particular reason why you were displaying the items and all
those MsgBox dialogs? That would slow down the procedure hugely.
 
G

Guest

Great! Thanks Sue,

I am displaying that info just so that I know I can refer
to that info. What I eventually want to do is fax the
attachments in these e-mails. The e-mail subject line or
message body will contain the persons name. I hope to be
able to use the info in either the subject or message
body to pass to the faxing tool in XP, then fax the
attachment, and move to the next e-mail. Currently I
spend about 1 hour at the fax machine every morning. :(
I have to print out each attachment, and then fax it to
the correct person.

If you have any more tips on how to accomplish this, I
would appreciate it.

Thanks again Sue.

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top