PDA

View Full Version : Solved: Help with automatically moving emails to folders based on sendername



oneillp111
06-28-2013, 11:04 AM
Hope someone can help.
I am trying to organize my emails as they come to my inbox. I would like to have the emails get sent to a folder based on the senderName. If there isn't a folder then the program should create one. I have a code that I got from a website, jpsoftwaretech.com, I also have the two functions it calls for as modules, listed as the second code. When I get an email nothing happens, and i get no errors. I am using 2010 and I do not know much about macros just looking for a little assistance. THANKS.

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace

' set object reference to default Inbox
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
' fires when new item added to default Inbox
' (per Application_Startup)

On Error GoTo ErrorHandler

Dim Msg As Outlook.MailItem
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim targetFolder As Outlook.MAPIFolder
Dim senderName As String

' don't do anything for non-Mailitems
If TypeName(item) <> "MailItem" Then GoTo ProgramExit

Set Msg = item

' move received email to target folder based on sender name


If CheckForFolder(Msg.senderName) = False Then ' Folder doesn't exist
Set targetFolder = CreateSubFolder(Msg.senderName)
Else
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set targetFolder = _
objNS.GetDefaultFolder(olFolderInbox).Folders(Msg.senderName)
End If

Msg.Move targetFolder


ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub



Second part, the functions:
Function CheckForFolder(strFolder As String) As Boolean
' looks for subfolder of specified folder, returns TRUE if folder exists.
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

' try to set an object reference to specified folder
On Error Resume Next
Set FolderToCheck = olInbox.Folders(strFolder)
On Error GoTo 0

If Not FolderToCheck Is Nothing Then
CheckForFolder = True
End If

ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function


Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder
' assumes folder doesn't exist, so only call if calling sub knows that
' the folder doesn't exist; returns a folder object to calling sub
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)

Set CreateSubFolder = olInbox.Folders.Add(strFolder)

ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function

oneillp111
07-16-2013, 05:52 AM
Code works, macros were disabled in Outlook

studmuffin
11-02-2014, 06:37 PM
I would like to use this code but it does not work for me. after it would work i would use a rule with category called "E-mail Completed"
What do i do with first part and second part. I also need to run as a script in outlook what would be the best declaration to use.

studmuffin
11-13-2014, 04:12 PM
Maybe I wasn't clear. I would like a macro that would automatically make folder by sender name. When email assigned a catergory it would use a rule with script to move completed email to senders folder. Please respond. I been looking everywhere for this.

studmuffin
12-01-2014, 07:07 PM
Can someone respond please this would be beneficial to many outlook users.

skatonni
12-02-2014, 03:44 PM
http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_posthelp_faq_item

"... If you are still stuck, create a new post and link to the thread you found so others can see what you have already tried."

Mason_Baumga
12-17-2014, 08:27 AM
It's possible I wasn't crystal clear. I'd really like some sort of macro that could automatically produce folder through sender label. As soon as mail given some sort of catergory it'd start using a concept along with software to move completed mail to senders folder. Make sure you answer. I already been hunting all over the place due to this.