Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Check Display Names or Email in MS Outlook using Excel VBA


In outlook Application sometimes we wanted to check the given name is valid or not. Also sometimes we need to check the email address is exist, if exist we need to check the display name.This code will help you to achieve this activity.

'----------------------------------------------------------------------------------
' This function will return valid or invalid
'-----------------------------------------------------------------------------------
Public Function checkdisplayname(name_email) As String

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(name_email)
oRecip.Resolve
If oRecip.Resolved Then
checkdisplayname = "Valid"
Else
checkdisplayname = "Invalid"
End If

End Function

'----------------------------------------------------------------------------------
' This function will return email address if valid, else blank value will be returned
'-----------------------------------------------------------------------------------
Public Function checkdisplayname_email(name_email) As String
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(name_email)
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
checkdisplayname_email = oEU.PrimarySmtpAddress
End If
Case 10, 30 'olOutlookContactAddressEntry & 'olSmtpAddressEntry
checkdisplayname_email = oRecip.AddressEntry.Address
End Select
End If
End Function

Sub checknames()

email_or_name = "support"
MsgBox checkdisplayname(email_or_name)
MsgBox checkdisplayname_email(email_or_name)

End Sub

Subscribe

Enter your e-mail below and get notified on the latest blog posts.




Tags

VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts


Follow Us