0

Hello I have a VBA script that loops through a spreadsheet and finds Employees that that are not assigned and send that employee and email to notify them. In the past I have had a list of emails (Outlook GAL) that works fine, but as the list grows I have been trying to get the email to pull from outlook by referencing the name from the loop. I have found this code online:

Function ResolveDisplayNameToSMTP(sFromName) As String
    ' takes a Display Name (i.e. "James Smith") and turns it into an email address (james.smith@myco.com)
    ' necessary because the Outlook address is a long, convoluted string when the email is going to someone in the organization.
    ' source:  https://stackoverflow.com/questions/31161726/creating-a-check-names-button-in-excel

    Dim OLApp As Object 'Outlook.Application
    Dim oRecip As Object 'Outlook.Recipient
    Dim oEU As Object 'Outlook.ExchangeUser
    Dim oEDL As Object 'Outlook.ExchangeDistributionList

    Set OLApp = CreateObject("Outlook.Application")
    Set oRecip = OLApp.Session.CreateRecipient(sFromName)
    oRecip.Resolve
    If oRecip.Resolved Then
        Select Case oRecip.AddressEntry.AddressEntryUserType
            Case 0, 5 'olExchangeUserAddressEntry & olExchangeRemoteUserAddressEntry
                Set oEU = oRecip.AddressEntry.GetExchangeUser
                If Not (oEU Is Nothing) Then
                    ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress
                End If
            Case 10, 30 'olOutlookContactAddressEntry & 'olSmtpAddressEntry
                Dim PR_SMTP_ADDRESS As String
                PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
                ResolveDisplayNameToSMTP = oRecip.AddressEntry.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
        End Select
    End If
End Function

But on the line oRecip.Resolve I get "Run-time error 287" Application-defined or object-defined error" and can't figure out what is going wrong. I have seen it may be a security permission but I can't find a setting that would be causing it and have VBA creating and sending emails from other scripts.

Can anyone help me resolve this error, or if you know of another way to find in company outlook emails from first and last names I'm open to using a different method.

Thank You

Trae
  • 119
  • 8

0 Answers0