I've written some VB code to get an email message from the Inbox of a group mailbox on the Exchange Server (without using Outlook) and move it to a subfolder. I'm having difficulty getting the email to move.
I have two functions, one returns the folder's ID and the other does the moving. Here is the function to capture the folder ID:
Public Function GetFolderID(FolderName As String) As String
Dim lcdoFolders As Folders
Dim lcdoFolder As Folder
Dim lbFound As Boolean
Dim lstrFolderID As String
On Error GoTo ErrorHandler
Set lcdoFolders = gcdoInbox.Folders
lbFound = False
Set lcdoFolder = lcdoFolders.GetFirst
Do While (Not lbFound) And Not (lcdoFolder Is Nothing)
Debug.Print lcdoFolder.Name
If lcdoFolder.Name = FolderName Then
lbFound = True
lstrFolderID = lcdoFolder.FolderID
Debug.Print lstrFolderID
Exit Do
Else
Set lcdoFolder = lcdoFolders.GetNext
lstrFolderID = ""
End If
Loop
Set lcdoFolder = Nothing
Set lcdoFolders = Nothing
GetFolderID = lstrFolderID
Exit Function
ErrorHandler:
MsgBox Err.Number & " " & Err.Description
If Not (lcdoFolder Is Nothing) Then
Set lcdoFolder = Nothing
End If
If Not (lcdoFolders Is Nothing) Then
Set lcdoFolders = Nothing
End If
Exit Function
End Function
This function is supposed to move the mail to a subfolder but doesn't. The MoveToFldrID argument was populated by the function above.
Public Function MoveEmail(MoveToFldrID As String) As Boolean
Dim lcdoMovedMsg As Message
On Error GoTo ErrorHandler
Set lcdoMovedMsg = gcdoMessage.MoveTo(MoveToF
ldrID)
Set lcdoMovedMsg = Nothing
MoveEmail = True
Exit Function
ErrorHandler:
MsgBox Err.Number & " " & Err.Description
If Not (lcdoMovedMsg Is Nothing) Then
Set lcdoMovedMsg = Nothing
End If
MoveEmail = False
Exit Function
GetFolderID is called from a Sub and its return value is used by MoveEmail in the same Sub. In that same Sub, the gcdoMessage object is set as such:
Set gcdoInbox = gcdoSession.Inbox
Set gcdoMessages = gcdoInbox.Messages
Set gcdoMessage = gcdoMessages.GetFirst
The lcdoMovedMsg contains the Subject of the message that is being moved. Does anybody have any idea why the message is not getting moved. I'm at a loss.
Start Free Trial