#1
|
|||
|
|||
Auto-sort new message macro based on domain
I found a macro online that works mostly, but has glitch. When an email comes in from someone using a different domain name, the macro either creates a new folder if there is not a current folder with the domain name, or files that message in an existing folder with the domain name. The issue is that new folders are being created for messages coming in with the domain name, and I'm not too familiar with MS Outlook macros to know what to do. Any help would be appreciated!
Code:
Public Sub SortByDomain(oMsg As MailItem) On Error Resume Next Dim sDomain As String 'The Sender's domain Dim oNS As Outlook.NameSpace 'My namespace Dim oInbox As Outlook.MAPIFolder 'My Inbox Dim oTarget As Outlook.MAPIFolder 'The domain folder 'If it's not your domain, decipher the domain. If InStr(oMsg.SenderEmailAddress, "@mydomain.com") < 1 Then sDomain = Mid(oMsg.SenderEmailAddress, InStr(oMsg.SenderEmailAddress, "@") + 1) Else sDomain = "mydomain.com" End If 'Get the inbox. Set oNS = Application.GetNamespace("MAPI") Set oInbox = oNS.GetDefaultFolder(olFolderInbox) 'Set the domain folder if it exists. Set oTarget = oInbox.Folders(sDomain) 'In case the folder doesn't exist... If oTarget Is Nothing Then oInbox.Folders.Add (sDomain) Set oTarget = oInbox.Folders(sDomain) End If 'Move the new mail to the folder. oMsg.Move oTarget 'Cleanup. Set oTarget = Nothing Set oInbox = Nothing Set oNS = Nothing End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Form auto fill based on a different field value. | stct | Word | 26 | 06-15-2019 04:08 PM |
Looking for Help to Create a Macro (Sort) | rsrasc | Word VBA | 5 | 04-16-2014 03:25 AM |
Sort table using macro | saslotteroy | Word VBA | 2 | 09-15-2011 02:41 PM |
Date auto-populates based on checkbox | mcarter9000 | Word VBA | 5 | 12-23-2010 12:39 PM |
message rule based on headers (smtp alias) | robr | Outlook | 1 | 11-18-2010 07:54 AM |