Consulting

Results 1 to 7 of 7

Thread: Solved: Help with automatically moving emails to folders based on sendername

  1. #1

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

    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.

    [VBA]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

    [/VBA]

    Second part, the functions:
    [VBA]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

    [/VBA]

  2. #2
    Code works, macros were disabled in Outlook

  3. #3
    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.

  4. #4
    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.

  5. #5
    Can someone respond please this would be beneficial to many outlook users.

  6. #6
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    http://www.vbaexpress.com/forum/faq....thelp_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."
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  7. #7
    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.
    You can easily check out our best quality 600-199 brain dumps exam dumps prepare you well for the real testking.net - 98-361 brain dumps You can also get success in real exam of tabor with the quality Pittsburg State University questions and ccie voice Tabor College answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •