View Single Post
  #1  
Old July 18th 07, 06:07 AM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 1
Default Move email to folder based on ReceivedTime

I have some code that I've attach to a menu button that will
move a selected email to a hard coded predefined folder. I would
like the code to look at the ReceivedTime of the selected email and
set the target objFolder to based on the ReceivedTime. If the
ReceivedTime is say '01/02/2007 03:35:40 PM' I want to set the
objFolder to 200702-In. I can get the folder name with the following
line of
code:

sbDateStr = Mid(objItem.ReceivedTime, 7, 4) & Mid
(objItem.ReceivedTime, 4, 2) & "-In"

I just can't figure out how to set objFolder to the value stored in
sbDateStr. I'd also like to have an error routine to detect if
200702-In exists before attempting t move the email. Here's the code
I have so far.

Sub sbmovemsgs()

Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
Dim sbDateStr As String
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objFolder = Application.GetNamespace("MAPI").Folders("Personal
Folders").Folders("200707-In")
'Set objFolder = objInbox.Parent.Folders("Stuart")
'Assume this is a mail folder

If objFolder Is Nothing Then
MsgBox "This folder doesn't exist!", vbOKOnly +
vbExclamation, "INVALID FOLDER"
End If

If Application.ActiveExplorer.Selection.Count = 0 Then
'Require that this procedure be called only when a message is selected
Exit Sub
End If

For Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move objFolder
'how do I set objFolder to the value in sbDateStr below
'sbDateStr = Mid(objItem.ReceivedTime, 7, 4) &
Mid(objItem.ReceivedTime, 4, 2) & "-In"
End If
End If
Next

Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing

End Sub



Thanks
Stu

Ads