Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2014, 10:26 AM
wtnelso wtnelso is offline Auto-sort new message macro based on domain Windows 7 64bit Auto-sort new message macro based on domain Office 2010 64bit
Novice
Auto-sort new message macro based on domain
 
Join Date: May 2014
Posts: 1
wtnelso is on a distinguished road
Default 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

Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-sort new message macro based on domain 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
Auto-sort new message macro based on domain 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft