How can I move messages?

  • Thread starter Thread starter michaaal
  • Start date Start date
M

michaaal

I want to move messages that are in my InBox that come from: John Davis
[[email protected]]
into a folder (that already exists) called InBox/Davis Group.
I also want to move messages that are in my InBox that come from: Mary
Keller [[email protected]]
into a folder (that already exists) called InBox/Southwest Pakton.

I wish to do this programmatically and not using the Rules Wizard.

Could someone post some code that would do this?
 
Use this class module as a starting point for manipulating incoming messages
with VBA:

Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE

'EXAMPLES:

'SAVE ATTACHMENT
Item.SaveAs "C:\Temp\mymessage.txt", olTXT

'MOVE MESSAGE TO SPECIFIED FOLDER
Item.Move myFolderObject

'CHECK SUBJECT PROPERTY
If Item.Subject = "text I'm looking for" Then
'Do THIS
End If

'CHECK THE FROM ADDRESS
'(See http://www.slipstick.com/dev/code/getsenderaddy.htm for more info)

'ETC., ETC.
End Sub

Just instantiate this class in the Application_Startup event of the
ThisOutlookSession module (Dim myNewClass as New <NameOfClassModuleAbove>).
Add additional code in the NewMailItems_ItemAdd event to handle the current
Item object as you see fit (i.e. use the Move method to move it to a
specified folder, save attachments to a local folder, etc.).

If you are not a programmer, see http://www.slipstick.com/addins/auto.htm
for some utilities that may help you.
 
Eric Legault said:
Use this class module as a starting point for manipulating incoming messages
with VBA:

No no. I am not interested in "incoming" messages! Only messages that are
currently in my InBox. Thank you for the info, but this is not what I asked
for! Any other ideas?




Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE

'EXAMPLES:

'SAVE ATTACHMENT
Item.SaveAs "C:\Temp\mymessage.txt", olTXT

'MOVE MESSAGE TO SPECIFIED FOLDER
Item.Move myFolderObject

'CHECK SUBJECT PROPERTY
If Item.Subject = "text I'm looking for" Then
'Do THIS
End If

'CHECK THE FROM ADDRESS
'(See http://www.slipstick.com/dev/code/getsenderaddy.htm for more info)

'ETC., ETC.
End Sub

Just instantiate this class in the Application_Startup event of the
ThisOutlookSession module (Dim myNewClass as New
Add additional code in the NewMailItems_ItemAdd event to handle the current
Item object as you see fit (i.e. use the Move method to move it to a
specified folder, save attachments to a local folder, etc.).

If you are not a programmer, see http://www.slipstick.com/addins/auto.htm
for some utilities that may help you.


--
Eric Legault, B.A., MCP, MCSD, MVP - Outlook
ImagiNET Resources Corp.
http://www.imaginets.com


michaaal said:
I want to move messages that are in my InBox that come from: John Davis
[[email protected]]
into a folder (that already exists) called InBox/Davis Group.
I also want to move messages that are in my InBox that come from: Mary
Keller [[email protected]]
into a folder (that already exists) called InBox/Southwest Pakton.

I wish to do this programmatically and not using the Rules Wizard.

Could someone post some code that would do this?
 
The procedure below should do what you need with some modification:

Sub MoveMessagesFromCertainPeopleToSpecifiedFolder()
Dim objNS As Outlook.NameSpace
Dim objItem As Object, objInbox As Outlook.MAPIFolder
Dim objMessage As Outlook.MailItem, objDestinationFolder1 As
Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

For Each objItem In objInbox.Items
If objItem.Class = olMail Then
Set objMessage = objItem

'YOUR HOMEWORK IS TO GO TO:
'http://www.slipstick.com/dev/code/getsenderaddy.htm
'AND ADD CODE TO CHECK THE E-MAIL ADDRESS OF EACH MESSAGE IN THE
FOLDER
'TO SEE IF IT IS FROM THE PEOPLE THAT YOU ARE LOOKING FOR

'ALTERNATELY, YOU CAN USE THE FIND OR RESTRICT METHOD ON THE
ITEMS COLLECTION
'TO LOOK FOR SPECIFIC MESSAGES

'IF THIS IS THE PERSON, MOVE IT TO THE MySubFolder FOLDER IN
YOUR INBOX:
Set objDestinationFolder1 = objInbox.Folders("MySubFolder")
objMessage.Move objDestinationFolder1
End If
Next
End Sub

--
Eric Legault, B.A., MCP, MCSD, MVP - Outlook
ImagiNET Resources Corp.
http://www.imaginets.com


michaaal said:
Eric Legault said:
Use this class module as a starting point for manipulating incoming messages
with VBA:

No no. I am not interested in "incoming" messages! Only messages that are
currently in my InBox. Thank you for the info, but this is not what I asked
for! Any other ideas?




Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE

'EXAMPLES:

'SAVE ATTACHMENT
Item.SaveAs "C:\Temp\mymessage.txt", olTXT

'MOVE MESSAGE TO SPECIFIED FOLDER
Item.Move myFolderObject

'CHECK SUBJECT PROPERTY
If Item.Subject = "text I'm looking for" Then
'Do THIS
End If

'CHECK THE FROM ADDRESS
'(See http://www.slipstick.com/dev/code/getsenderaddy.htm for more info)

'ETC., ETC.
End Sub

Just instantiate this class in the Application_Startup event of the
ThisOutlookSession module (Dim myNewClass as New
Add additional code in the NewMailItems_ItemAdd event to handle the current
Item object as you see fit (i.e. use the Move method to move it to a
specified folder, save attachments to a local folder, etc.).

If you are not a programmer, see http://www.slipstick.com/addins/auto.htm
for some utilities that may help you.


--
Eric Legault, B.A., MCP, MCSD, MVP - Outlook
ImagiNET Resources Corp.
http://www.imaginets.com


michaaal said:
I want to move messages that are in my InBox that come from: John Davis
[[email protected]]
into a folder (that already exists) called InBox/Davis Group.
I also want to move messages that are in my InBox that come from: Mary
Keller [[email protected]]
into a folder (that already exists) called InBox/Southwest Pakton.

I wish to do this programmatically and not using the Rules Wizard.

Could someone post some code that would do this?
 
Back
Top