A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Move email to folder based on ReceivedTime



 
 
Thread Tools Search this Thread Display Modes
  #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

  #2  
Old July 18th 07, 07:59 AM posted to microsoft.public.outlook.program_vba
Michael Bauer [MVP - Outlook]
external usenet poster
 
Posts: 1,885
Default Move email to folder based on ReceivedTime


Because Selection.Count=0 means no action I'd put that on top. In general
(also within the loop): Don't do unnecessary actions if the requirements
aren't given.

Then use one variable for the parent's Folders collection:

Dim ParentFolders as outlook.Folders
Set ParentFolders=objNS.Folders("Personal Folders").Folders

As your target folder might change for each objItem, the ref to it must be
set within the For Each loop:

For Each ...
If objItem.Class = olmail Then
sbDateStr = ....
Set objFolder=ParentFolders(sbDateStr)
If not objFolder is nothing then
If objFolder.DefaultItemType = olMailItem Then
...
Else
MsgBox "Folder '" & sbDateStr & "' doesnt exist"
Endif
Endif
Next

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Organize eMails:
http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6

Am Wed, 18 Jul 2007 05:07:16 -0000 schrieb :

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

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Move email messages to a specified folder based on a category DougLost Outlook and VBA 2 October 7th 10 03:18 PM
A rule for creating a folder based on sender's name and move messages to it hprod Outlook - General Queries 3 February 21st 07 11:21 PM
move msgs based on one website Move msgs based on website Outlook - Using Contacts 0 November 30th 06 10:28 AM
Move emails to a folder rule (Time based) George Lake Outlook - General Queries 1 April 27th 06 03:09 PM
Move mail to a folder based on 2 different words in the subject fi Alan Kirkham Outlook and VBA 2 February 14th 06 02:34 PM


All times are GMT +1. The time now is 10:44 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.