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:
  1. Option Explicit
  2. 'Add a reference to MS Outlook xx.0 Object Library
  3. Private Sub Command1_Click()
  4.  
  5.     Dim oApp As Outlook.Application
  6.     Dim oInbox As Outlook.MAPIFolder
  7.     Dim oEmail As Object
  8.    
  9.     Set oApp = New Outlook.Application
  10.     'Reference the default Inbox
  11.     Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
  12.     For Each oEmail In oInbox.Items
  13.         'Make sure its a mailitem and not a meeting request or read receipt etc.
  14.         If oEmail.Class = olMail Then
  15.             'Get the senders email address
  16.             MsgBox oEmail.SenderEmailAddress
  17.         End If
  18.     Next
  19.     'Clean up
  20.     Set oEmail = Nothing
  21.     Set oInbox = Nothing
  22.     oApp.Quit
  23.     Set oApp = Nothing
  24. End Sub