move a specified email "From" tag items to a certain folder whenever there is "New Mail" in the inbo

Status
Not open for further replies.
What I want to do is move a specified email "From" tag items to a certain folder whenever there is "New Mail" in the inbox.


Private Sub Application_NewMail()
Dim BillsEmailFrom(100), JunkEmailFrom(100), ForumsEmailFrom(100) As String
Dim numbills, numjunk, numforums As Double
Dim OLapp As New Outlook.Application
Dim OLns As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim Bills As Outlook.MAPIFolder
Dim JunkEmail As Outlook.MAPIFolder
Dim Forums As Outlook.MAPIFolder
Dim mail As String
BillsEmailFrom(1) = "bill1"
BillsEmailFrom(2) = "bill2"
BillsEmailFrom(3) = "bill3"
numbills = 3
JunkEmailFrom(1) = "advertize@droz.com"
JunkEmailFrom(2) = "advertize@vitahealth.com"
numjunk = 2
Set OLns = OLapp.GetNamespace("MAPI")
Set Inbox = OLns.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items
Set PersonalFolder= Inbox.Folders("Personal Folders")
Set BillsFolder = PersonalFolder.Folders("Bills")
Set JunkFolder = PersonalFolder.Folders("Junk E-mail")
If Inbox.Items.Count = 0 Then Exit Sub
For iLoop = 1 To Inbox.Items.Count
For nb = 1 To numbills
If Inbox.Items(iLoop).From = BillsEmailFrom(nb) Then Item.Move BillsFolder
Next nb
For nj = 1 To numjunk
If Inbox.Items(iLoop).From = JunkEmailFrom(nj) Then Item.Move JunkFolder
Next nj
Next iLoop
Set ForumsFolder = Nothing
Set JunkFolder = Nothing
Set BillsFolder = Nothing
Set Items = Nothing
Set Inbox = Nothing
Set OLns = Nothing
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
There are three errors:

1) The Item variable doesn't point to the item of the loop.

2) You couldn't move item(1) in the nb loop and then again reference it in the next nj loop as it wouldn't exist anymore in that folder. So the outer loop should be for the address type, the inner one for the folder items.

3) Loop backwards through a collection if you want to remove items from it.
 
Set PersonalFolder= Inbox.Folders("Personal Folders")
is wrong. Personal Folders is where every folder is in including Inbox.
I tried:
Set PersonalFolder = OLns.GetDefaultFolder("Personal Folders")
There are two default folders then plus the syntax of the "Personal Folders"?
 
I get an error when I add this:

Set PersonalFolder = OLns.GetFolders
Set Inbox = OLns.GetDefaultFolder(olFolderInbox)
Set BillsFolder = PersonalFolder.Folders("Bills")
Set JunkFolder = PersonalFolder.Folders("Junk E-mail")
 

oliv-

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
try this
Set Inbox = OLns.GetDefaultFolder(olFolderInbox)
Set BillsFolder = Inbox.Folders("Bills")
Set JunkFolder = Inbox.Folders("Junk E-mail")
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
M Error: This mailbox exceeded the maximum number of corrupted items that were specified for this move Exchange Server Administration 9
P Move emails between 2 mailboxes. Using Outlook 1
C Copy Move item won't work Outlook VBA and Custom Forms 2
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
Commodore Move turns into "copy" Using Outlook 3
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
Jennifer Murphy Ctrl+Tab sometimes will not move through text a word at a time Using Outlook 1
V Outlook 2016 will not move emails in search results Using Outlook 4
M move to iCloud not working in outlook calendar Using Outlook 12
A Create date folder and move messages daily Outlook VBA and Custom Forms 1
Commodore Folders always closed in move/copy items dialog box Using Outlook 3
C Move Outlook 2007 to new PC with Outlook 365 Using Outlook 3
C Can't move folder, the folder is full Using Outlook 0
Nadine Rule to move attachments with specific name Outlook VBA and Custom Forms 1
A Move email items based on a list of email addresses Outlook VBA and Custom Forms 40
T Move calendar invites to new calendar Using Outlook 5
O Rule to move (specific) messages from Sent folder to Specific folder Using Outlook 1
I Automating message move between folders Outlook VBA and Custom Forms 0
K Outlook Rules: Move a Copy Using Outlook 4
K VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
N Move emails of same conversation to same subfolder Using Outlook 6
P when i move inbox mails to another folder in outlook the mail disappears Using Outlook 1
Peter H Williams How to Move Mail item after processing Outlook VBA and Custom Forms 5
D Move Emails between Folders in Separate Mailbox Outlook VBA and Custom Forms 12
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
I Outlook 2013 Any way to move .OST file to new system? Using Outlook.com accounts in Outlook 5
O On click,I want to change subject line of selected mail and then reply to particular email and move Using Outlook 3
F "Move to" O365 feature to Outlook client via VBA Outlook VBA and Custom Forms 4
B Macro to manually move selected emails to network folder Outlook VBA and Custom Forms 1
L Making rule to move email to folder from one O365 domain Using Outlook 1
B Copy/Move Exchange inbox to Pop inbox Using Outlook 4
S Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
C Move from Outlook 2007 Enterprise (MOE) to Outlook Pro plus 2007 Using Outlook 1
Cdub27 Move Outlook 2016 Contacts to Other Folders - Extremely Slow !! Using Outlook 6
M code to move selected Outlook contacts to another folder Using Outlook 3
Mark White VBScript Move sent mail to non-default folder Outlook VBA and Custom Forms 5
Mark White VBScript Move sent mail to non-default folder Outlook VBA and Custom Forms 0
G Favorites panel entries move around after restarting Outlook 2016 Using Outlook 1
M Move new mail to folder based on sender address Outlook VBA and Custom Forms 2
D Help with code to move mail on receipt to another folder based on time received Outlook VBA and Custom Forms 2
S VBA Code to move mail items from search folder to inbox subfolder Outlook VBA and Custom Forms 4
acpete48317 Categorize and move Outlook Email Outlook VBA and Custom Forms 2
Q VBA Script to move item in secondary mailbox Outlook VBA and Custom Forms 2
F Move mails from Deleted Items folder back to its original folder where the mails got deleted Using Outlook 0
L pin to Outlook move mail Using Outlook 2
S Auto move search results to folder Outlook VBA and Custom Forms 0
K VBA to move emails in folder to a windows folder Outlook VBA and Custom Forms 2
N Move red and unflagged emails to subfolder Outlook VBA and Custom Forms 1
S Move email accounts to new laptop Using Outlook 1
S VBA to search a keyword in attachment and move to a folder Outlook VBA and Custom Forms 0
Similar threads


















































Top