title: I want to read SenderAddress from office 365 Outlook mail using VBA in excel?
body: <p>I have tried everything to read a mail from office 365 outlook but I am not able to read it. Every time Sender address is coming empty.
  Error That I am getting is :
  Run-time error: ‘287’
  Application-defined or object-defined error.</p>
  <p>Please find the code that I am using.</p>
  <pre><code>Option Explicit
  
  Sub Mail()
  
      Dim xNameSpace As Outlook.Namespace
      Dim xFolder As Outlook.Folder
      Dim xOutlookApp As Outlook.Application
      
      Set xOutlookApp = New Outlook.Application
      Set xNameSpace = xOutlookApp.Session
      Set xFolder = xNameSpace.GetDefaultFolder(olFolderInbox)
      
      Set xFolder = xFolder.Folders(&quot;Retail&quot;)
          ' Set Outlook application object.
      Dim objOutlook As Object
      Set objOutlook = CreateObject(&quot;Outlook.Application&quot;)
      
      Dim objNSpace As Object     ' Create and Set a NameSpace OBJECT.
      ' The GetNameSpace() method will represent a specified Namespace.
      Set objNSpace = objOutlook.GetNamespace(&quot;MAPI&quot;)
      
      Dim myFolder As Object  ' Create a folder object.
      Set myFolder = objNSpace.GetDefaultFolder(olFolderInbox)
      
      Dim objItem As Object
      Dim iRows, iCols As Integer
      iRows = 2
  
      ' Loop through each item in the folder.
      For Each objItem In xFolder.Items
          If objItem.Class = olMail Then
              Dim GetSenderAddress As String
              
              Dim objMail As Outlook.MailItem
              Set objMail = objItem
              
              Dim mailType As String
              mailType = objMail.SenderEmailType
              
              
              If mailType = &quot;EX&quot; Then
                  ' GetSenderAddress = GetExchangeSenderAddressNew(objMail)
                  FindAddress (objMail.SenderEmailAddress)
              Else
                  GetSenderAddress = objMail.SenderEmailAddress
              End If
  
              Cells(iRows, 1) = objMail.SenderEmailAddress
              Cells(iRows, 2) = objMail.To
              Cells(iRows, 3) = objMail.Subject
              Cells(iRows, 4) = objMail.ReceivedTime
          End If
          iRows = iRows + 1
      Next
      Set objMail = Nothing
     
      ' Release.
      Set objOutlook = Nothing
      Set objNSpace = Nothing
      Set myFolder = Nothing
  
  End Sub
  
  Private Function GetExchangeSenderAddress(objMsg As MailItem) As String
      
      Dim oSession As Object
      Set oSession = CreateObject(&quot;MAPI.Session&quot;)
      oSession.Logon &quot;&quot;, &quot;&quot;, False, False
      
      Dim sEntryID As String
      Dim sStoreID As String
      Dim oCdoMsg As Object
      Dim sAddress As String
      Const g_PR_SMTP_ADDRESS_W = &amp;H39FE001F
      
      sEntryID = objMsg.EntryID
      sStoreID = objMsg.Parent.StoreID
      Set oCdoMsg = oSession.GetMessage(sEntryID, sStoreID)
      
      sAddress = oCdoMsg.Sender.Fields(g_PR_SMTP_ADDRESS_W).Value
      Set oCdoMsg = Nothing
      oSession.Logoff
      Set oSession = Nothing
      
      GetExchangeSenderAddress = sAddress
      
  End Function
  </code></pre>
  <p>Another Code is:</p>
  <pre><code>Sub Mail()
  
      Dim jsObj As New ScriptControl
      
      jsObj.Language = &quot;JScript&quot;
      
      With jsObj
          .AddCode &quot;outlookApp = new ActiveXObject('Outlook.Application'); nameSpace = outlookApp.getNameSpace('MAPI'); nameSpace.logon('','',false,false); mailFolder = nameSpace.getDefaultFolder(6); var Inbox = mailFolder.Folders; var box = Inbox.Item('Retail').Items;  &quot;
      End With
      
      
  End Sub
  </code></pre>
  <p>Please let me know if i can read sender address of a mail in office 365 outlook.</p>