I am writing a code to sort emails to different outlook folders. I need to set a condition for emails that are both sent and received only by members of my company, but a condition with “And” or similar conditions don’t sort any emails at all into the folder. Added to that, using only the recipient address condition gets emails sorted that don’t only have my company as recipient. I would be grateful for any advice.
Sub emailfolder ()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim inmsg As Object
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olMessages = olFolder.Items
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim rAddress As String
Dim sAddress As String
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set myDestFolder1 =
objNS.Folders("[email protected]").Folders("client").Folders("2024").Folders("product").Folders("subproduct")
For Each inmsg In olMessages
If TypeOf inmsg Is MailItem Then
Set recips = inmsg.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
rAddress = LCase(pa.GetProperty(PR_SMTP_ADDRESS))
sAddress = LCase(SenderEmailAddress)
rLen = Len(rAddress) - InStrRev(rAddress, "@")
sLen = Len(sAddress) - InStrRev(sAddress, "@")
rRight = Right(rAddress, rLen)
sRight = Right(sAddress, sLen)
rLeft = Left(rRight, 3) 'my company uses 2 tld
sLeft = Left(sRight, 3)
If inmsg.subject Like "*clientx*" Or rRight = "clientx.com" Or sRight = "clientx.com" Then
If rLeft = "mycompany" And sLeft = "mycompany" Then
inmsg.Move myDestFolder1
End If
End If
Next recip
End If
Next inmsg
End Sub