Microsoft Office Tutorials and References
In Depth Information
The function MoveMessagesBySubject() is presented in Listing 25.1. Note that this function
calls the function FindFolder() , which was presented in Chapter 24. In other words,
FindFolder() must be in the same module as MoveMessagesBySubject() or you will get
an error message.
The MoveMessagesBySubject() Function
Public Function MoveMessagesBySubject(Subject As String, _
DestinationFolder As String) As Boolean
‘ Moves all messages from the Inbox to the specified folder
‘ if the message subject contains the text in the Subject
‘ argument.
‘ Returns True on success, False on error.
‘ Requires access to the function FindFolder() to run.
Dim fInbox As Outlook.Folder
Dim fDestination As Outlook.Folder
Dim m As Outlook.MailItem
Dim MyOutlookNamespace As Outlook.NameSpace
Dim FoldersToMove As New Collection
‘Obtain the required MAPI namespace.
Set MyOutlookNamespace = GetNamespace(“MAPI”)
‘ Set up error handling.
On Error GoTo ErrorHandler
‘ Get the references to the Inbox and destination folders.
Set fInbox = MyOutlookNamespace.GetDefaultFolder(olFolderInbox)
Set fDestination = FindFolder(DestinationFolder)
‘ If destination folder not found, display
‘ message and exit.
If fDestination Is Nothing Then
MsgBox (“The destination folder could not be found.”)
MoveMessagesBySubject = False
Exit Function
End If
‘ Now loop through the Inbox looking at each mesasage.
For Each m In fInbox.Items
If InStr(m.Subject, Subject) > 0 Then
‘ Add message to the “to be moved” collection.
FoldersToMove.Add m
End If
Search JabSto ::

Custom Search