PC Review
Forums
Newsgroups
Microsoft Outlook
Microsoft Outlook VBA Programming
Oddity in collection of "restricted" mail items
Forums
Newsgroups
Microsoft Outlook
Microsoft Outlook VBA Programming
Oddity in collection of "restricted" mail items
![]() |
Oddity in collection of "restricted" mail items |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 -- Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginners Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm "Robert Day" <robertday@hotmail.com> wrote in message news:63f953b1.0402160125.5cebb93a@posting.google.com... > 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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
I found the answer to my own question shortly after posting - it was
in a KB article at http://support.microsoft.com/defaul...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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

