Oddity in collection of "restricted" mail items

R

Robert Day

Hi

I am using this RnR code (foot of message) to grab the body text of
various messages from my Inbox, write it to a text file and then move
the processed messages to a separate folder. This works fine on a
machine at home running Internet mail (albeit only on a test set of 4
messages) but is proving problematic here at work on Windows 2000 /
Outlook 2000 / Exchange Mail (don't know Exchange version). The count
of matching messages is echoed correctly but for some reason it is not
capturing the body of all of them and is not moving all of them. As an
example the code below I created 4 messages with subject "TEST" and
body text "test1", "test2", etc. Running the macro showed a count of 4
messages to process (correct) but only captured the body of 3 messages
and only moved three messages. Can anyone see any obvious mistake I am
making in the code below (watch for wrapping)?

Sub ParseFormmail()
Dim appOL As Outlook.Application
Dim nmsNameSpace As Outlook.NameSpace
Dim fldFolder As Outlook.MAPIFolder
Dim strContent As String
Dim fsoSysObj As FileSystemObject
Dim txstream As TextStream

Set fsoSysObj = New FileSystemObject
Set txstream = fsoSysObj.OpenTextFile("C:\TestMsg.txt", ForWriting,
True)

Set appOL = CreateObject("Outlook.Application")
Set nmsNameSpace = appOL.GetNamespace("MAPI")
Set fldFolder = nmsNameSpace.GetDefaultFolder(olFolderInbox)
MsgBox "You must choose the folder to move the processed items to as
the next step" & _
vbCrLf & "Make sure it is somewhere other than the Inbox",
vbOKOnly
Set destFolder = nmsNameSpace.PickFolder

Set itmItems = fldFolder.Items

Set myItems = itmItems.Restrict("[Subject] = 'TEST'")
MsgBox "There are " & myItems.Count & " consultation messages that
will be processed.", vbInformation
For Each Item In myItems
strContent = Item.Body
MsgBox (strContent)
txstream.Write (strContent)
txstream.WriteBlankLines (2)
Item.Move (destFolder)
Next

txstream.Close
End Sub

Robert
 
K

Ken Slovak - [MVP - Outlook]

Never modify the contents of a collection you are iterating using a
For Each or count up For loop. Use a count down For loop instead:
For i = myItems.Count To 1 Step -1
 
R

Robert Day

I found the answer to my own question shortly after posting - it was
in a KB article at http://support.microsoft.com/default.aspx?scid=kb;EN-US;195699
.. Changed code accordingly and all is as it should be.

I also saw a response from Ken Slovak that had been harvested on
another web forum that explained the same. Thanks Ken - I have your
Sams book on Outlook 2000 and it has been very useful to me with other
dev work.

Robert
 

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