title: VBA Outlook script error: an object could not be found
body: <p>I am trying to use a VBA script to pull info from the emails in Inbox into an excel spreadsheet:</p>
  
  <pre><code>'Subject
  'To Address
  'From Address
  'CC Addresses
  </code></pre>
  
  <p>it fails for senders who have already left the organization and they're no longer in O365.</p>
  
  <p>This is the code bit:</p>
  
  <pre><code>Function X400toSMTP(strAdr As String) As String
  Dim olkRcp As Outlook.Recipient, olkUsr As Outlook.ExchangeUser
  Set olkRcp = Session.CreateRecipient(strAdr)
  
  If olkRcp.AddressEntry = Empty Then
      X400toSMTP = strAdr
  
  ElseIf olkRcp.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
      olkRcp.Resolve
      Set olkUsr = olkRcp.AddressEntry.GetExchangeUser
      X400toSMTP = olkUsr.PrimarySmtpAddress
  End If
  
  Set olkRcp = Nothing
  Set olkUsr = Nothing
  End Function
  </code></pre>
  
  <p>I ran the debug and it stops at <code>AddressEntry</code> : <code>The attempted operation failed. An object could not be found</code>
  I'm trying to find a way to make the script leave the address field empty for those senders who cannot be found on O365 anymore and further process the rest of the items in the Inbox.</p>
  
  <p>I have tried the below:</p>
  
  <pre><code>If IsNull(olkRcp.AddressEntry) Then
      X400toSMTP = strAdr
  </code></pre>
  
  <p>but am still getting the same error for AddressEntry.</p>
  
  <p>I am just a VBA noob so would very much appreciate your advice.</p>
  
  <p>Many thanks!</p>