Microsoft Office Tutorials and References
In Depth Information
‘ If any matching messages were found, move them.
If FoldersToMove.Count > 0 Then
For Each m In FoldersToMove
MsgBox “There are no messages to move.”
MoveMessagesBySubject = True
MoveMessagesBySubject = False
You should note two things about this function. First, it uses the function FindFolder() to
obtain a reference to the destination folder. This function was presented in Chapter 24. It must be
available in your Outlook project.
Second, you can see how the code uses a collection to move folders. As mentioned in Chapter 24,
it can cause problems if you move an item out of the Inbox (or any other folder) while the For
Each...Next loop is still looping through the folder. Instead, you should keep track of which
items are to be moved, and the Collection object is ideal for this purpose. Then, after the For
Each...Next loop has completed, you can move the items by going through the collection.
Some Possible Changes
As written, the function moves any message in which the message subject contains the specified
text. This is done using the Instr() function, which compares two strings:
If String2 is found anywhere within String1 , the function returns a value greater than 0 (in
fact, it returns the position at which String2 is found). Otherwise it returns 0. You could also
move messages only if the message subject exactly matches the specified text by changing that line
of code to:
If StrComp(m.Subject, Subject, vbTextCompare) = 0