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?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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.
 

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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
 

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Hendre Bezuidenhout

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server 2013
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
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
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
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 4
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 18
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 3
U Macro for reminders,tasks,calendar Outlook VBA and Custom Forms 4
V macro runs slower on startup than after Outlook VBA and Custom Forms 3
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Calendar Printing Assistant 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
O Run macro automatically at sending an email Using Outlook 11
R Retain Original Message When Forwarding With Macro Outlook VBA and Custom Forms 3
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
B Reply and replyall macro is not working Outlook VBA and Custom Forms 1
O Macro - paste as plain text Outlook VBA and Custom Forms 2
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
witzker Macro to set contact reminder to next day 9:00 Outlook VBA and Custom Forms 45
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
R VBA macro - new message Outlook VBA and Custom Forms 3
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
S VBA Macro - Run-time error '424': object required - Help Please Outlook VBA and Custom Forms 3

Similar threads

Top