[RESOLVED] Parsing SenderEmailAddress on incoming Items in Outlook 2003
I am trying to create some VBcode in Outlook 2003 that will inspect the SenderEmailAddress and route the email to the appropriate sub folder. As an example to this problem if an incoming email came from the SenderEmailAddress [email protected] I want to move all email from the domain of "@yahoo.com" to the sub folder of my inbox named Yahoo Mail. In trying to do this I ran into a problem where I am getting an error 424 object required. Is there another way to accomplish what I am looking to do? Any help would be greatly appreciated, here is what I have so far:
VB Code:
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim objNS As NameSpace
Dim objInbox As MAPIFolder
Dim objMoveFolder As MAPIFolder
Dim intDomainNameStart As Integer
Dim strUserName As String
Dim strDomainName As String
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
'--Sort by SenderEmailAddress--
intDomainNameStart = Item.SenderEmailAddress.IndexOf("@") 'I get the 424 Error Here
strUserName = Item.SenderEmailAddress.Substring(0, intDomainNameStart - 1)
strDomainName = Item.SenderEmailAddress.Substring(intDomainNameStart)
Select Case strDomainName
Case "@yahoo.com"
Set objMoveFolder = objInbox.Folders("Yahoo Mail")
'Case ""
'Set objMoveFolder = objInbox.Folders("").Folders("")
Case Else
'
End Select
If Not objMoveFolder Is Nothing Then
Item.Move objMoveFolder
End If
Set objMoveFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
End Sub
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
Welcome to the forums!
You could try using the InStr function to find the position of the "@".
VB Code:
intDomainNameStart = InStr(1, Item.SenderEmailAddress, "@")
1 Attachment(s)
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
You do know that you could use a "rule" to achieve this same result without any code?
Its on the Tools menu under "Rules and Alerts...".
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
I do know that but I have many more rules than just the one in the example that exceed the 32k limit on rules allowed. So I am trying to get around that by simulating the rules wizard through VBCode.
BTW the InStr function worked on that first line and now I am getting the same error for the subsequent two lines.
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
OK, so code is the way to go.
You are getting errors on the next two lines because of Substring, which is not a vba Function
I would suggest using the vba function "Mid" to get the domain and "Left" to get the UserName.
VB Code:
strUserName = Left(Item.SenderEmailAddres, intDomainNameStart - 1)
strDomainName = Mid(Item.SenderEmailAddress, intDomainNameStart)
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
DKenny,
That last bit of code did not fix the problem it gave me this error "outlook run-time error 438" Object does not support that method. The code may not have worked but it set me on the right path to fix the code. I change the code so that the parsing looks like this:
VB Code:
'--Sort by SenderEmailAddress--
strSenderEmailAddress = Item.SenderEmailAddress
intSenderEmailAddressLen = Len(strSenderEmailAddress)
intDomainNameStart = InStr(1, strSenderEmailAddress, "@")
strUserName = Left(strSenderEmailAddress, intDomainNameStart - 1)
strDomainName = Mid(strSenderEmailAddress, intDomainNameStart)
I would like to thank you for your help with this problem!! :bigyello:
Re: Parsing SenderEmailAddress on incoming Items in Outlook 2003
Not a probnlem, just glad you were able to sort it now. Please don't forget to mark this thread as resolved (its under Thead tools at the top of the page).