move read email based on date and sender

  • Thread starter Thread starter vbaNEWBIE
  • Start date Start date
V

vbaNEWBIE

Hopefully someone here can help....I would like to move items from my inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
 
Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:
 
Thanks Michael - so that I understand I would change it to the following:
Set myItem = myItems.Find("[SenderName] = 'PersonName' AND [Unread] =
'false' AND [SentOn] <= 'DateToMove'")

Did I move the quotation marks correctly ?

Will have to search on using a Date with a Find function.

thanks !

Michael Bauer said:
Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:
Hopefully someone here can help....I would like to move items from my inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
.
 
Another thought on the Date issue.....is there a way to utilize the Date
function in the filter so that you could use something like If DateReceived <
(Date - 14) Then move to another folder ?

I have used the (Date - 14) within VBA for Excel and it works great and if
this would work it would allow for basically a variable to be used rather
than a specific date. This would be helpful if someone wanted to regularly
run the macro to move emails older than 14 days without having to code it
differently each time.

Thoughts ?

Michael Bauer said:
Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:
Hopefully someone here can help....I would like to move items from my inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
.
 
If the filter isn't as flexible as you need it for the date, filter the
items just for SenderName and Unread. Then loop through the result, read
each item's SentOn property, and do your calculation with it.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 06:22:02 -0800 schrieb vbaNEWBIE:
Another thought on the Date issue.....is there a way to utilize the Date
function in the filter so that you could use something like If DateReceived <
(Date - 14) Then move to another folder ?

I have used the (Date - 14) within VBA for Excel and it works great and if
this would work it would allow for basically a variable to be used rather
than a specific date. This would be helpful if someone wanted to regularly
run the macro to move emails older than 14 days without having to code it
differently each time.

Thoughts ?

Michael Bauer said:
Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:
Hopefully someone here can help....I would like to move items from my inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
.
 
hhhmmmm.....thanks Michael.......what would the code look like for that ? I
am new to VBA with Outlook so I do not have my legs under me yet.

Any suggestions ?



Michael Bauer said:
If the filter isn't as flexible as you need it for the date, filter the
items just for SenderName and Unread. Then loop through the result, read
each item's SentOn property, and do your calculation with it.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 06:22:02 -0800 schrieb vbaNEWBIE:
Another thought on the Date issue.....is there a way to utilize the Date
function in the filter so that you could use something like If DateReceived <
(Date - 14) Then move to another folder ?

I have used the (Date - 14) within VBA for Excel and it works great and if
this would work it would allow for basically a variable to be used rather
than a specific date. This would be helpful if someone wanted to regularly
run the macro to move emails older than 14 days without having to code it
differently each time.

Thoughts ?

Michael Bauer said:
Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:

Hopefully someone here can help....I would like to move items from my
inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the
code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
.
.
 
Date functions in Outlook are the same as in Excel.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 13:15:01 -0800 schrieb vbaNEWBIE:
hhhmmmm.....thanks Michael.......what would the code look like for that ? I
am new to VBA with Outlook so I do not have my legs under me yet.

Any suggestions ?



Michael Bauer said:
If the filter isn't as flexible as you need it for the date, filter the
items just for SenderName and Unread. Then loop through the result, read
each item's SentOn property, and do your calculation with it.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 06:22:02 -0800 schrieb vbaNEWBIE:
Another thought on the Date issue.....is there a way to utilize the Date
function in the filter so that you could use something like If DateReceived <
(Date - 14) Then move to another folder ?

I have used the (Date - 14) within VBA for Excel and it works great and if
this would work it would allow for basically a variable to be used rather
than a specific date. This would be helpful if someone wanted to regularly
run the macro to move emails older than 14 days without having to code it
differently each time.

Thoughts ?

:



Modify the filter like this: [SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:

Hopefully someone here can help....I would like to move items from my
inbox
based on a specific situation. I have the code below for part of it
but
I
need to add more code to complete it. I am using Outlook 2007 and the
code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
.
.
 
I would like to know if you ever figured out the Date problem. I have a very basic code that has incorporated portions of this but I cannot get the most recent email from a Specific Sender to pull up. The Macro always pulls up the oldest email.

Here is the line I am struggling with:

Set myItem = myItems.Find("[SenderName] = 'REPORT WRITER' AND [SentOn] = 'Date' AND [Subject] = 'GENERIC'
")
Hopefully someone here can help....I would like to move items from my inbox
based on a specific situation. I have the code below for part of it but I
need to add more code to complete it. I am using Outlook 2007 and the code
will run from a Module when initiated by the User.


I would like to move items if meets ALL three conditions:
(1) comes from the Sendername coded below - AND -
(2) the email has been READ - AND -
(3) and the sent date is 14 days old or older



Can anyone help me understand how to modify the code below to accomplish
this ?


Sub MoveItems_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items

Set myDestFolder =
Outlook.Session.Folders("PersonalFolder").Folders("PersonNameFolder")
Set myItem = myItems.Find("[SenderName] = 'PersonName'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
[SenderName] = 'x' AND [Unread] = 'false' AND
[SentOn] <= '...'

Please see the VBA help for how to use a date with the Find function.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 16 Feb 2010 08:45:01 -0800 schrieb vbaNEWBIE:

inbox
code
Set myItem = myItems.Find("[SenderName] = 'PersonName' AND [Unread] =
'false' AND [SentOn] <= 'DateToMove'")

Did I move the quotation marks correctly ?

Will have to search on using a Date with a Find function.

thanks !

"Michael Bauer [MVP - Outlook]" wrote:
..is there a way to utilize the Date
function in the filter so that you could use something like If DateReceived <
(Date - 14) Then move to another folder ?

I have used the (Date - 14) within VBA for Excel and it works great and if
this would work it would allow for basically a variable to be used rather
than a specific date. This would be helpful if someone wanted to regularly
run the macro to move emails older than 14 days without having to code it
differently each time.

Thoughts ?

"Michael Bauer [MVP - Outlook]" wrote:
On Wednesday, February 17, 2010 12:08 PM Michael Bauer [MVP - Outlook] wrote:
If the filter is not as flexible as you need it for the date, filter the
items just for SenderName and Unread. Then loop through the result, read
each item's SentOn property, and do your calculation with it.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 06:22:02 -0800 schrieb vbaNEWBIE:

DateReceived <
regularly
AND
I
..thanks Michael.......what would the code look like for that ? I
am new to VBA with Outlook so I do not have my legs under me yet.

Any suggestions ?



"Michael Bauer [MVP - Outlook]" wrote:
On Thursday, February 18, 2010 2:56 AM Michael Bauer [MVP - Outlook] wrote:
Date functions in Outlook are the same as in Excel.

--
Best regards
Michael Bauer - MVP Outlook
Manage and share your categories:
<http://www.vboffice.net/product.html?pub=6&lang=en>


Am Wed, 17 Feb 2010 13:15:01 -0800 schrieb vbaNEWBIE:

I
if
rather
it
but
accomplish
 
I would like to know if you ever figured out the Date problem. I have a very basic code
that has incorporated portions of this but I cannot get the most recent email from a
Specific Sender to pull up. The Macro always pulls up the oldest email.

Here is the line I am struggling with:

Set myItem = myItems.Find("[SenderName] = 'REPORT WRITER' AND [SentOn] = 'Date' AND [Subject] = 'GENERIC'")

As Michael Bauer wrote earlier: have a look at the help pages how to use a date with the .Find method.

Your code, as written, cannot work as intended. At minimum, it should look this:
Set myItem = myItems.Find("[SenderName] = 'REPORT WRITER' AND [SentOn] = " & strDate & " AND [Subject] = 'GENERIC'")
where strDate is a string representation of the date you're interested in. For the needed format of such a string,
you should inspect the .SentOn property of any mail item.
 
Back
Top