Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get this script not to move emails less than 30 days old ?

Status
Not open for further replies.

RonQA

Technical User
Jun 24, 2007
61
US
The script below moves Read emails to the Personal Folder creating a folder with the sender's name then puts the email in that folder. I can't seem to come up with a way to leave emails 30 days old or less alone and do not move them. Outlook 2003

Can anyone help?

======================

Sub Folders()

Const strPersonalMain As String = "Personal Folders"
Const strPersonalInbox As String = "00 All Sort"

Dim oaoutlook As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim fldInbox, fldNew, fldPersonal, fldInbox2 As MAPIFolder
Dim MailItem As MailItem
Dim intX As Integer
Dim strFolderName As String


On Error Resume Next

Set oaoutlook = CreateObject("Outlook.Application")
Set myNameSpace = oaoutlook.GetNamespace("MAPI")
Set fldInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

Set fldPersonal = myNameSpace.Folders(strPersonalMain)
Set fldInbox2 = fldPersonal.Folders(strPersonalInbox)

For intX = 1 To fldInbox.Items.Count
Set MailItem = fldInbox.Items(intX)

If MailItem.Class = olMail And MailItem.UnRead = False Then
strFolderName = MailItem.SenderName

Err.Clear

Set fldNew = fldInbox2.Folders(strFolderName)

If Err.Number <> 0 Then
Set fldNew = fldInbox2.Folders.Add(strFolderName)
End If

MailItem.Move fldNew
Set MailItem = Nothing
End If
Next intX
End Sub

============================

Thanks for any help!
 
Hi,

Open the Object Browser (F2) and find MailItem in Classes. Listed in the 'Members of MailItem' are all the related properties and methods. You ought to be able to find what you are looking for.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,

Thanks for the quick response..What I am struggling with is the actual "IF" statement.

"If" the email is 30days or less then do nothing with it "else" move it.

any ideas?

Thanks,
 
"If" the email is 30days or less then do nothing with it "else" move it.

[
code]
If [the email] > 30 then
' do comething with it
End If


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here is what I having trouble with.

The code finds the "Read" email then moves it.

I want it to find only "Read" emails 30 days old and move those specific emails.

How would I change the code I have to accomplish this?

Thanks,
 
Something like (just had a quick look at the MailItem class so is just initial thoughts):
Code:
 If MailItem.Class = olMail And MailItem.UnRead = False and (DateDiff("d",MailItem.ReceivedTime,Date) > 30) Then...
As I say, untested but should give you somewhere to start from.

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Thanks so much... it worked great.
 
Glad I could help [smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top