I've been having problems using the CopyTo and MoveTo methods available for mail items using CDO 1.21 library.
The error occurs in the LfnMove routine below. I am not sure how to code the MailItem.CopyTo or which ID property I may need to use - but every attempt just seems to spit back 'object does not support this property or method'.
I assume that I'm doing something really dumb!
Thanks in advance.
Private Sub CmdTest_Click()
Dim objOLApp As Object, objFolder As Object, objItem As Object
Dim OlMapi As Object, MstFolder As Object, SubFolder As Object
Set objOLApp = CreateObject("Outlook.Application"
Set OlMapi = objOLApp.GetNamespace("MAPI"
Set MstFolder = OlMapi.Folders("Phase 11 - 2004 Mail"
LfnSweepFolder MstFolder.Folders("InBox"
, "Incoming"
LfnSweepFolder MstFolder.Folders("Sent Items"
, "Outgoing"
End Sub
Private Sub LfnSweepFolder(Fldr As Object, MailTyp As String)
Dim MailItem As Object, Who As String
For Each MailItem In Fldr.items
Select Case LCase(TypeName(MailItem))
Case "mailitem"
With MailItem
Debug.Print "Folder: " & Fldr.Name
Debug.Print "To: " & .To
Debug.Print "From: " & .sendername
Debug.Print "Subject: " & .Subject
Debug.Print "Received: " & .ReceivedTime
Debug.Print "Will be moved to: " & LfnDestination(MailItem, MailTyp)
LfnMove MailItem, LfnDestination(MailItem, MailTyp), Fldr.Parent
End With
End Select
Next
Set MailItem = Nothing
End Sub
'**********************************************************
'* Deduce the logical destination folder for a mail item *
'* depending upon sender/recipient and/or subject *
'**********************************************************
Private Function LfnDestination(MailItem As Object, MailTyp As String) As String
Dim Where As String, Subject As String
If LCase(MailTyp) = "incoming" Then
Who = LCase(MailItem.sendername)
Else
Who = LCase(MailItem.To)
End If
Subject = LCase(MailItem.Subject)
Select Case True
Case InStr(Who, "fred"
: Where = "Office"
Case InStr(Who, "john"
: Where = "Admin"
etc
End Select
'Sender/recipient no good, try subject
If Where = "" Then
Select Case True
Case InStr(Subject, "sql server"
: Where = "Microsoft"
Case InStr(Subject, "high st"
: Where = "Office"
Case InStr(Subject, "zb360"
: Where = "Junk"
End Select
If Where = "" Then Where = "**Unknown**"
End If
LfnDestination = Where
End Function
Private Sub LfnMove(MailItem As Object, Destn As String, ParentFldr As Object)
Dim ErrNo As Long, DestMsg As Object
On Error Resume Next
Set DestMsg = MailItem.CopyTo(ParentFldr.Folders(Destn).ID)
ErrNo = Err.Number
On Error GoTo 0
If ErrNo = -2147221233# Then 'folder does not exist
ParentFldr.Folders.Add Destn
Set DestMsg = MailItem.CopyTo(ParentFldr.Folders(Destn))
Else
Err.Raise ErrNo
End If
End Sub
The error occurs in the LfnMove routine below. I am not sure how to code the MailItem.CopyTo or which ID property I may need to use - but every attempt just seems to spit back 'object does not support this property or method'.
I assume that I'm doing something really dumb!
Thanks in advance.
Private Sub CmdTest_Click()
Dim objOLApp As Object, objFolder As Object, objItem As Object
Dim OlMapi As Object, MstFolder As Object, SubFolder As Object
Set objOLApp = CreateObject("Outlook.Application"
Set OlMapi = objOLApp.GetNamespace("MAPI"
Set MstFolder = OlMapi.Folders("Phase 11 - 2004 Mail"
LfnSweepFolder MstFolder.Folders("InBox"
LfnSweepFolder MstFolder.Folders("Sent Items"
End Sub
Private Sub LfnSweepFolder(Fldr As Object, MailTyp As String)
Dim MailItem As Object, Who As String
For Each MailItem In Fldr.items
Select Case LCase(TypeName(MailItem))
Case "mailitem"
With MailItem
Debug.Print "Folder: " & Fldr.Name
Debug.Print "To: " & .To
Debug.Print "From: " & .sendername
Debug.Print "Subject: " & .Subject
Debug.Print "Received: " & .ReceivedTime
Debug.Print "Will be moved to: " & LfnDestination(MailItem, MailTyp)
LfnMove MailItem, LfnDestination(MailItem, MailTyp), Fldr.Parent
End With
End Select
Next
Set MailItem = Nothing
End Sub
'**********************************************************
'* Deduce the logical destination folder for a mail item *
'* depending upon sender/recipient and/or subject *
'**********************************************************
Private Function LfnDestination(MailItem As Object, MailTyp As String) As String
Dim Where As String, Subject As String
If LCase(MailTyp) = "incoming" Then
Who = LCase(MailItem.sendername)
Else
Who = LCase(MailItem.To)
End If
Subject = LCase(MailItem.Subject)
Select Case True
Case InStr(Who, "fred"
Case InStr(Who, "john"
etc
End Select
'Sender/recipient no good, try subject
If Where = "" Then
Select Case True
Case InStr(Subject, "sql server"
Case InStr(Subject, "high st"
Case InStr(Subject, "zb360"
End Select
If Where = "" Then Where = "**Unknown**"
End If
LfnDestination = Where
End Function
Private Sub LfnMove(MailItem As Object, Destn As String, ParentFldr As Object)
Dim ErrNo As Long, DestMsg As Object
On Error Resume Next
Set DestMsg = MailItem.CopyTo(ParentFldr.Folders(Destn).ID)
ErrNo = Err.Number
On Error GoTo 0
If ErrNo = -2147221233# Then 'folder does not exist
ParentFldr.Folders.Add Destn
Set DestMsg = MailItem.CopyTo(ParentFldr.Folders(Destn))
Else
Err.Raise ErrNo
End If
End Sub