PDA

View Full Version : Outlook 2007 VBA - Optainer full email address of Sender



paddysheeran
07-28-2011, 07:04 AM
Hi all,

after a lot of searches round the net for an answer I have not been able to find a full procedure on the code and actions needed to store the email senders full address (e.g. samplename@samplehome.com) in a variable for search and count purposes.

My Code current reads:

Public ReportingYear, ReportingMonth, ReportingMonthNumeric, Date_Month, From_Status As String
Dim objOLMail As Object
Sub SHLNG_Emails()
Dim objOutlook As Object, ns As Object, objFolder, Inbox, objOLMail As Object
Dim EmailCount As Integer

Set ns = GetNamespace("MAPI")
Date_Calculations
Date_Month = ReportingMonthNumeric & " " & ReportingMonth

Set Inbox = ns.Folders("Mailbox - Custom Service Desk G").Folders("Inbox").Folders("Archive").Folders(ReportingYear).Folders(Date_Month)
For Each objOLMail In Inbox.Items
From_Status = objOLMail.SenderEmailAddress

If InStr(objOLMail.SenderEmailAddress, "@Sample1Address.com") > 0 Then
EmailCount = EmailCount + 1

End If
Next


If EmailCount = 0 Then
MsgBox "There were no emails received in this reporting period.", vbInformation, _
"Nothing Found"
Exit Sub
End If
MsgBox "There were " & EmailCount & " emails received in this reporting period.", vbInformation, _
"Emails Found"
End Sub

Instead of the SenderEmailaddress which reads something like /O=EXCHANGE/OU=BTUK01/CN=RECIPIENTS/CN=SUPPLIST I need the full address so I can performe the search:


If InStr(objOLMail.SenderEmailAddress, "@Sample1Address.com") > 0 Then
EmailCount = EmailCount + 1


I believe there is some fuction available to do this but im unsure how to merge this with my code to gain the results I need.

many thanks in advance.

Paddy.

Charlize
07-29-2011, 02:01 AM
First check the type of mailaddress. Exchange or smtp. Little example.
Sub get_mail_address()
Dim mymessage As Outlook.MailItem
'make sure it's a mailitem and nothing else (ie. read receipt or something else)
Set mymessage = ActiveExplorer.Selection.Item(1)
Select Case mymessage.SenderEmailType
Case "EX"
'Problem we have when using internal mail is strange
'bunch of tokens in emailaddress. Therefor we determine the
'kind of emailtype that is used, ie. EXchange (internal) or outside SMTP
'This code gives the username that is used for sending from
'the domain. Just add the domain after the name
MsgBox Mid(Split(mymessage.SenderEmailAddress, "/")(4), _
InStr(Split(mymessage.SenderEmailAddress, "/")(4), "=") + 1)
Case "SMTP"
'this is external mail
MsgBox mymessage.SenderEmailAddress
End Select
End SubCharlize

paddysheeran
07-29-2011, 05:22 AM
Hi thanks for that it provides the data I need. I just need to know now how to encorporate this into my existing code.

paddysheeran
08-11-2011, 02:14 AM
can any help?

JP2112
08-12-2011, 12:53 PM
Right after "For Each objOLMail In Inbox.Items" replace "From_Status = objOLMail.SenderEmailAddress " with this:



Select Case objOLMail.SenderEmailType
Case "EX"
'Problem we have when using internal mail is strange
'bunch of tokens in emailaddress. Therefor we determine the
'kind of emailtype that is used, ie. EXchange (internal) or outside SMTP
'This code gives the username that is used for sending from
'the domain. Just add the domain after the name
From_Status = Mid(Split(objOLMail.SenderEmailAddress, "/")(4), _
InStr(Split(objOLMail.SenderEmailAddress, "/")(4), "=") + 1)
Case "SMTP"
'this is external mail
From_Status = objOLMail.SenderEmailAddress
End Select