Macro to file Outlook email by sender's email address

Status
Not open for further replies.

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
Can someone help me with changing the macro "Macro to file Outlook email by sender's display name" in order to file the emails by sender's email address?
 
Hi Diane,

Yes I was looking at the one at slipstick.com

Problem is if the sender sends from multiple PC's from the same account and the "Name" is not the same then you have multiple folders but emails are from the same abc.abc@abc.com for eg.
 
Diane,
it possible to group emails per sender in sub folders sent from the same domain?

If I have for eg. more than one person from a company sending me emails "xx@au.abb.com", zz@au.abb.com" I want all from xx in one folder another from zz and the root folder under inbox show for eg. au.abb.com

Thanks
 
It is possible, but it requires more code. you'll need to use regex or instr to get the address and create the folders. I'd probably file by domain only and use group by views to group by the sender address using the method here: http://www.slipstick.com/exchange/adding-extended-mapi-fields-to-outlook/

This edit should get the part after the @, in your example, it would be au.abb.com
Code:
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
 
sSenderName= Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sSenderName)

i think this snippet will work to use a domain.com\alias@domain.com folder format - i didn't test it, but if it doesn't work, its very close to what you need.

Code:
Dim objDomainFolder As Outlook.Folder
   If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
On Error Resume Next 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain)
      End If 
 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName) 
       End If 
            objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder= Nothing 
        End If 
    End If 
        Err.Clear 
    Next

Keep in mind that filing by sender makes a mess of threads involving multiple people, although Outlook's conversation view mitigates that somewhat.

Edited to fix the code - was missing on error resume next's and clearing objDomainFolder
 
I think I created a nice loop.

all 10146 emails gave me a message box (Moved) and had to press ok, after that all 10146 messages opened in outlook and did not move.

Code:
Public Sub MoveSelectedMessages() 
    Dim objOutlook As Outlook.Application 
    Dim objNamespace As Outlook.NameSpace 
    Dim objDestFolder As Outlook.MAPIFolder 
    Dim objSourceFolder As Outlook.Folder 
    Dim currentExplorer As Explorer 
    Dim Selection As Selection 
    Dim obj As Object 
   
   Dim objVariant As Variant 
    Dim lngMovedItems As Long 
    Dim intCount As Integer 
    Dim intDateDiff As Integer 
    Dim strDestFolder As String
  Set objOutlook = Application 
    Set objNamespace = objOutlook.GetNamespace("MAPI") 
    Set currentExplorer = objOutlook.ActiveExplorer 
    Set Selection = currentExplorer.Selection 
    Set objSourceFolder = currentExplorer.CurrentFolder 
   
   For Each obj In Selection 
        Set objVariant = obj 
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
     
 
On Error Resume Next 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain) 
 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName) 
       End If 
            objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder = Nothing 
        End If 
    End If 
        Err.Clear 
      
       ' Display the number of items that were moved. 
  MsgBox "Moved " & lngMovedItems & " messages(s)." 
   
   Set currentExplorer = Nothing 
    Set obj = Nothing 
    Set Selection = Nothing 
    Set objOutlook = Nothing 
    Set objNamespace = Nothing 
    Set objSourceFolder = Nothing 
    Next 
  
   End Sub
 
Ouch. They shouldn't open, there is nothing in the code to open them.

This moves: objVariant.Move objDomainFolder
it would be objVariant.display to open them.

You're missing an End If here:
If objDestFolder Is Nothing Then
Set objDestFolder = objSourceFolder.Folders.Add(sDomain)
end if
On Error Resume Next

My curiosity got the best of me after I posted it, and i tested it. My first try created the domain.com folders but put all the mail in one folder - because of the missing Set objDomainFolder = Nothing, which i came back and added.
 
This one works well, but only moves some messages.

Code:
Public Sub MoveSelectedMessages() 
    Dim objOutlook As Outlook.Application 
    Dim objNamespace As Outlook.NameSpace 
    Dim objDestFolder As Outlook.MAPIFolder 
    Dim objSourceFolder As Outlook.Folder 
    Dim currentExplorer As Explorer 
    Dim Selection As Selection 
    Dim obj As Object 
   
   Dim objVariant As Variant 
    Dim lngMovedItems As Long 
    Dim intCount As Integer 
    Dim intDateDiff As Integer 
    Dim strDestFolder As String
  Set objOutlook = Application 
    Set objNamespace = objOutlook.GetNamespace("MAPI") 
    Set currentExplorer = objOutlook.ActiveExplorer 
    Set Selection = currentExplorer.Selection 
    Set objSourceFolder = currentExplorer.CurrentFolder 
   
   For Each obj In Selection 
        Set objVariant = obj 
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
     
 
On Error Resume Next 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain) 
 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName) 
       End If 
            objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder = Nothing 
        End If 
    End If 
        Err.Clear 
  Next 
        ' Display the number of items that were moved. 
  MsgBox "Moved " & lngMovedItems & " messages(s)." 
   
   Set currentExplorer = Nothing 
    Set obj = Nothing 
    Set Selection = Nothing 
    Set objOutlook = Nothing 
    Set objNamespace = Nothing 
    Set objSourceFolder = Nothing 
    End Sub
 
Getting a compile error "end if without block if"
Code:
Public Sub MoveSelectedMessages() 
    Dim objOutlook As Outlook.Application 
    Dim objNamespace As Outlook.NameSpace 
    Dim objDestFolder As Outlook.MAPIFolder 
    Dim objSourceFolder As Outlook.Folder 
    Dim currentExplorer As Explorer 
    Dim Selection As Selection 
    Dim obj As Object 
   
   Dim objVariant As Variant 
    Dim lngMovedItems As Long 
    Dim intCount As Integer 
    Dim intDateDiff As Integer 
    Dim strDestFolder As String
  Set objOutlook = Application 
    Set objNamespace = objOutlook.GetNamespace("MAPI") 
    Set currentExplorer = objOutlook.ActiveExplorer 
    Set Selection = currentExplorer.Selection 
    Set objSourceFolder = currentExplorer.CurrentFolder 
   
   For Each obj In Selection 
        Set objVariant = obj 
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
     
 
On Error Resume Next 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain) 
End If 
 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName) 
End If 
            objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder = Nothing 
        End If 
    End If 
        Err.Clear 
  Next
       ' Display the number of items that were moved. 
  MsgBox "Moved " & lngMovedItems & " messages(s)." 
   
   Set currentExplorer = Nothing 
    Set obj = Nothing 
    Set Selection = Nothing 
    Set objOutlook = Nothing 
    Set objNamespace = Nothing 
    Set objSourceFolder = Nothing 
  
   End Sub
 
you have too many End if's - you added one here:
If objDestFolder Is Nothing Then
Set objDestFolder = objSourceFolder.Folders.Add(sDomain)
On Error Resume Next

But didn't remove one of the two at the end.
Set objDomainFolder = Nothing
End If
End If
Err.Clear
Next

This works here -

Code:
Public Sub MoveSelectedMessages() 
    Dim objOutlook As Outlook.Application 
    Dim objNamespace As Outlook.NameSpace 
    Dim objDestFolder As Outlook.MAPIFolder 
    Dim objSourceFolder As Outlook.Folder 
    Dim currentExplorer As Explorer 
    Dim Selection As Selection 
    Dim obj As Object 
  
   Dim objVariant As Variant 
    Dim lngMovedItems As Long 
    Dim intCount As Integer 
    Dim intDateDiff As Integer 
    Dim strDestFolder As String
   Set objOutlook = Application 
    Set objNamespace = objOutlook.GetNamespace("MAPI") 
    Set currentExplorer = objOutlook.ActiveExplorer 
    Set Selection = currentExplorer.Selection 
    Set objSourceFolder = currentExplorer.CurrentFolder 
  
   For Each obj In Selection 
        Set objVariant = obj 
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
    
 
On Error Resume Next 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain) 
End If 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName) 
       End If 
            objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder = Nothing 
        End If 
        Err.Clear 
  Next 
        ' Display the number of items that were moved. 
  MsgBox "Moved " & lngMovedItems & " messages(s)." 
  
   Set currentExplorer = Nothing 
    Set obj = Nothing 
    Set Selection = Nothing 
    Set objOutlook = Nothing 
    Set objNamespace = Nothing 
    Set objSourceFolder = Nothing 
    End Sub
 
Hi Diane, I am getting there...

works but only moves the first message in inbox to the folder and subfolder then stops.

Code:
Public Sub MoveSelectedMessages() 
    Dim objOutlook As Outlook.Application 
    Dim objNamespace As Outlook.NameSpace 
    Dim objDestFolder As Outlook.MAPIFolder 
    Dim objSourceFolder As Outlook.Folder 
    Dim currentExplorer As Explorer 
    Dim Selection As Selection 
    Dim obj As Object 
   
   Dim objVariant As Variant 
    Dim lngMovedItems As Long 
    Dim intCount As Integer 
    Dim intDateDiff As Integer 
    Dim strDestFolder As String
  Set objOutlook = Application 
    Set objNamespace = objOutlook.GetNamespace("MAPI") 
    Set currentExplorer = objOutlook.ActiveExplorer 
    Set Selection = currentExplorer.Selection 
    Set objSourceFolder = currentExplorer.CurrentFolder 
   
   For Each obj In Selection 
        Set objVariant = obj 
    If objVariant.Class = olMail Then 
       intDateDiff = DateDiff("d", objVariant.SentOn, Now) 
         ' I'm using 40 days, adjust as needed. 
       If intDateDiff > 4 Then 
         sSenderName = objVariant.SenderEmailAddress 
      End If 
     
 
On Error Resume Next 
sDomain = Right(sSenderName, Len(sSenderName) - InStr(1, sSenderName, "@")) 
 
Set objDestFolder = objSourceFolder.Folders(sDomain) 
 
If objDestFolder Is Nothing Then 
    Set objDestFolder = objSourceFolder.Folders.Add(sDomain) 
End If 
 
On Error Resume Next 
Set objDomainFolder = objDestFolder.Folders(sSenderName) 
 
If objDomainFolder Is Nothing Then 
    Set objDomainFolder = objDestFolder.Folders.Add(sSenderName)
           objVariant.Move objDomainFolder 
            'count the # of items moved 
            lngMovedItems = lngMovedItems + 1 
            Set objDestFolder = Nothing 
  Set objDomainFolder = Nothing 
        End If 
    End If 
        Err.Clear 
  Next
       ' Display the number of items that were moved. 
  MsgBox "Moved " & lngMovedItems & " messages(s)." 
   
   Set currentExplorer = Nothing 
    Set obj = Nothing 
    Set Selection = Nothing 
    Set objOutlook = Nothing 
    Set objNamespace = Nothing 
    Set objSourceFolder = Nothing 
  
   End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
P Macro to attach a file in a shared Outlook draft folder Outlook VBA and Custom Forms 2
R Macro to check file name with outlook address book Outlook VBA and Custom Forms 0
J Outlook Macro to Update Sharepoint Excel File Using Outlook 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
E Copying data from e-mail attachement to EXCEL file via macro Outlook VBA and Custom Forms 38
Matt Johnson Save File Macro Working Sporadically Using Outlook 9
C Help with a Macro to move emails to a different PST data file Using Outlook 4
E Macro to export BCM business data to CSV file BCM (Business Contact Manager) 1
W Macro to File Inbox Messages? Using Outlook 1
B Simple way to insert file text - macro? Outlook VBA and Custom Forms 1
J Macro to create folder in PST file Outlook VBA and Custom Forms 4
Q A Macro for 'File - Save.As' on multiple selections Outlook VBA and Custom Forms 1
J Outlook macro to run before email is being send Outlook VBA and Custom Forms 0
H Macro to Delete Duplicate items in Outlook calendar where title is the same and date is the same Outlook VBA and Custom Forms 0
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 1
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 0
S macro error 4605 Outlook VBA and Custom Forms 0
A Macro Mail Alert Using Outlook 4
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to check Cursor & Focus position Outlook VBA and Custom Forms 8
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
M Outlook 2019 Macro not working Outlook VBA and Custom Forms 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
V Macro to count flagged messages? Using Outlook 2
sophievldn Looking for a macro that moves completed items from subfolders to other subfolder Outlook VBA and Custom Forms 7
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Macro to block senders domain Outlook VBA and Custom Forms 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
J Macro to Insert a Calendar Outlook VBA and Custom Forms 8
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
D Autosort macro for items in a view Outlook VBA and Custom Forms 2

Similar threads

Back
Top