Creating a “Check Names” button in Excel

后端 未结 1 576
暗喜
暗喜 2020-12-06 23:40

I am new to using VBA and Macros and am wondering if there is a way to add a \"check names\" function in Excel (similar to the function in Outlook). Part of the form I am w

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 00:15

    A couple of answers here:

    Edit: Created in Excel 2010 (no idea if it'll work in 2003).

    The first will return TRUE or FALSE if the name can be resolved in Outlook.

    '----------------------------------------------------------------------------------
    ' Procedure : ResolveDisplayNameToSMTP
    ' Author    : Sue Mosher - updated by D.Bartrup-Cook to work in Excel late binding.
    '-----------------------------------------------------------------------------------
    Public Function ResolveDisplayName(sFromName) As Boolean
    
        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
            ResolveDisplayName = True
        Else
            ResolveDisplayName = False
        End If
    
    End Function
    

    The second will resolve the name and return the email address:

    '----------------------------------------------------------------------------------
    ' Procedure : ResolveDisplayNameToSMTP
    ' Author    : Sue Mosher - updated by D.Bartrup-Cook to work in Excel late binding.
    '-----------------------------------------------------------------------------------
    Public Function ResolveDisplayNameToSMTP(sFromName) 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(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
                        ResolveDisplayNameToSMTP = oRecip.AddressEntry.Address
            End Select
        End If
    End Function
    

    Here's a test procedure showing how to use both functions:

    Sub Test()
    
        MsgBox ResolveDisplayName("Marty Moesta")
        MsgBox ResolveDisplayNameToSMTP("Marty Moesta")
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题