Thanks for hanging in there with me on this JP. I used your code and then changed the folders as you advised. I believe I have that part correct. I also used a valid email address to test that already had emails in the Inbox that should be moved to the folder specified. I am not sure which line of code to change as you also advised to declare MAPIFolder Objects as Folder Objects instead. I placed all the code into the ThisOutlookSession area. I thought it may only work on startup so I closed down Outlook and restarted and it did not move the email. I thought it may work upon receipt of the email and tried that with no luck. I then tried creating a Module and putting all the code there and tried those two scenarios and still no luck. Can you review the code below to see if I missed something ? Also, where specifically should the VBA code go into Outlook, into a Module or ThisOutlookSession or a combination ?
[vba]
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' (1) default Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
[/vba]
and then ....
[vba]
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
' (2) only act if it's a MailItem
Dim Msg As Outlook.MailItem
Dim fldr As Outlook.MAPIFolder
If TypeName(item) = "MailItem" Then
Set Msg = item
If Msg.SenderEmailAddress = "
Myname@somewhere.com" Then
' Original example - Set fldr = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("My Messages").Folders("My Folder")
Set fldr = Outlook.Session.Folders("TestDataFileNAME").Folders("TestDataFileNAMEfolder ")
' Actual path - TestDataFile\TestDataFileNAME\TestDataFileNAMEfolder
Msg.Move fldr
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
[/vba]
I figure I am missing something simple. Does this code run upon startup or does it run upon receipt ? I noticed when I placed the code into a module I could not find it when I hit F8 to run the macro.
Sorry to bother you but I tried it and tried it and cannot figure this out.
I did find one piece of code that I got to work once I had your help figuring out the folder issue. That code is below and it proves that the folder settings are correct now.
[vba]
Sub MoveItems_EmailAddress()
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 = myInbox.Folders("Personal Mail")
Set myDestFolder = Outlook.Session.Folders("TestDataFileNAME").Folders("TestDataFileNAMEfolder ")
Set myItem = myItems.Find("[SenderEmailAddress] = 'Myemail@somewhere.com'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
[/vba]
So for instructional purposes, are these two pieces of code equivalent ? I have a suspicion that the original code is supposed to run upon receipt or startup whereas the one above runs upon running the macro.
Thoughts ? I appreciate your help so much :-)