Outlook GetSelectNamesDialog questions

bnj1776

Board Regular
Joined
Aug 20, 2014
Messages
67
I'm trying to display the SelectNamesDialog with a subset of the Contacts to allow the user to pick one so that later I can pull all of that contacts information into Excel.

First, I'm getting a list of contacts that have been marked as "Customers" via the contact field User1

Then I'm adding each of those contacts to the "InitialAddressList" to the SelectNamesDialog.

But this is only filling in the "To" field with initial values, not limiting the Contact list to only those I'm wanting to show.


Not what I'm needing, so questions:

Is it possible to limit the displayed Contacts list (not the "To" field which will be hidden with NumberOfRecipientSelectors = olShowNone)?

If it is not, would it be possible to create a temporary contacts folder with copies of just the entries I need and then use that folder instead of the default when displaying the Select Names Dialog window?

Or is there a better way I should be looking at???

Thank you,
Brian


Code:
Option Explicit

Sub Contact_User1()
    
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
    
    Dim objNamespace As Object
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    
[COLOR=#008000]'   Check for the default address list in the Contacts folder[/COLOR]
    On Error GoTo ErrorHandler
    Dim objAddressList As Outlook.AddressList
    For Each objAddressList In Outlook.Application.Session.AddressLists
        If objAddressList.AddressListType = olOutlookAddressList Then
            If objAddressList.GetContactsFolder.EntryID = objNamespace.GetDefaultFolder(olFolderContacts).EntryID Then
                Exit For
            End If
        End If
    Next
    On Error GoTo 0
    
[COLOR=#008000]'   Find the desired Customer group[/COLOR]
    Dim objContacts As Object
    Set objContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items
    Dim objItems As Object
    Set objItems = objContacts.Restrict("[User1] = 'Customer'")
    Dim objItem As Object
    Dim sContacts As String
[COLOR=#008000]'   sContacts = vbNullString
'   For Each objItem In objItems
'       If (objItem.Class = olContact) Then
'           If sContacts <> vbNullString Then
'               sContacts = sContacts & "; "
'           End If
'           sContacts = sContacts & objItem.Email1Address
'       End If
'   Next
'   Debug.Print sContacts 'These are the address to choose from[/COLOR]


    
    Dim objDialog As Object
    Set objDialog = objOutlook.Session.GetSelectNamesDialog


    With objDialog
        .Caption = "Select Customer Contact"
        .ShowOnlyInitialAddressList = True
        .AllowMultipleSelection = False
        .NumberOfRecipientSelectors = olShowTo[COLOR=#008000] 'change to olShowNone[/COLOR]
        For Each objItem In objItems
            If (objItem.Class = olContact) Then
                .recipients.Add (objItem.Email1Address)
                .InitialAddressList.AddressEntries.Add (objItem.Email1Address)
            End If
        Next
        .Display
    End With


    Set objAddressList = Nothing
    Set objContacts = Nothing
    Set objItems = Nothing
    Set objItem = Nothing
    Set objDialog = Nothing
    
ErrorHandler:
    Exit Sub
End Sub
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Watch MrExcel Video

Forum statistics

Threads
1,122,915
Messages
5,598,850
Members
414,263
Latest member
sherrcha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top