Many times you may want/need to retrieve the senders email address from an email message in Outlook. We can do this many ways but here we are using the Outlook Object Model.
Note: This will invoke the dreaded infamouse Outlook Security Prompt because we are accessing a secured property of the MailItem.
Outlook And Visual Basic 6.0
VB Code:
Option Explicit
'Add a reference to MS Outlook xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Object
Set oApp = New Outlook.Application
'Reference the default Inbox
Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each oEmail In oInbox.Items
'Make sure its a mailitem and not a meeting request or read receipt etc.
If oEmail.Class = olMail Then
'Get the senders email address
MsgBox oEmail.SenderEmailAddress
End If
Next
'Clean up
Set oEmail = Nothing
Set oInbox = Nothing
oApp.Quit
Set oApp = Nothing
End Sub