?
Solved

Launching Select Names Dialog from a VBA dialog

Posted on 2005-04-06
1
Medium Priority
?
751 Views
Last Modified: 2008-02-01
Hello Experts.

Please does anyone know how to write a line of VBA code that will launch the select names outlook dialogue box. This is the one that appears when you write a new e-mail and click the "To..." button to choose recipients. Having launched this, I also need to retrieve the list of selected names from the "To ->", "Cc ->" and "Bcc ->" boxes when the dialog is closed.

Any help on this would be greatly appreciated.
0
Comment
Question by:Oli999
1 Comment
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 13716959
First, set a reference (in any code module, Tools Menu:Reference) to Microsoft CDO Object Library and Microsoft Outlook Object Library.
Then, here's the function, you'll have to slice it-dice it to suit your needs.

Hope this helps.
-Jim
______________

Public Function fn_outlook_select_names(ByRef oMail As MailItem) As Outlook.MailItem
   
'Display the Microsoft Outlook 'Select Names' dialog to let user pick names
'http://www.outlookcode.com/d/code/selectnames.htm

'Not available in Outlook object model, must use CDO, but return an Outlook MailItem.
'(What a pain...)

On Error GoTo error_handler

Dim oOutlook As Outlook.Application
Set oOutlook = CreateObject("Outlook.Application")

Dim oReturnMail As Outlook.MailItem
Set oReturnMail = oOutlook.CreateItem(olMailItem)

Dim x As Long

' start CDO session
Dim oSession As MAPI.Session
Set oSession = CreateObject("MAPI.Session")
oSession.Logon , , False, False

'FIXME: Figure out how to feed current To:Cc:Bcc settings into form

Dim sMessage As String

'Show address book (Cancel button creates an error that needs to be trapped)
On Error Resume Next
Dim cCDORecipients As MAPI.Recipients
Set cCDORecipients = oSession.AddressBook(, "Select Names", , , 3, "To", "Cc", "Bcc")

If Err = 0 Then
   
    ' put CDO recipients into Outlook message
    Dim cRecipients As Recipients, oRecipient As Recipient
    Dim oCDORecipient As MAPI.Recipient
    For Each oCDORecipient In cCDORecipients
       
        Set oRecipient = cRecipients.Add(oCDORecipient.AddressEntry.Address)
       
        If Err = 287 Then
            ' security block triggered
            sMessage = "Outlook cannot add recipients, because you clicked No on the "
            sMessage = sMessage & "e-mail address access dialog. You need to try again and click Yes "
            sMessage = sMessage & "this time."
            MsgBox sMessage, vbOKOnly, "E-mail Address Access"
            Exit For
        End If
       
        cRecipients.ResolveAll
    Next
   
ElseIf Err = -2147221229 Then
    'User canceled the address book dialog, do nothing or provide a message to user
End If

Set oMail = Nothing

For x = 1 To cCDORecipients.Count
    Select Case cCDORecipients.Item(x).Type
        Case 1
            If x = 1 Then oReturnMail.To = ""
            oReturnMail.To = oReturnMail.To & "; " & cCDORecipients(x).Name
        Case 2
            If x = 2 Then oReturnMail.CC = ""
            oReturnMail.CC = oReturnMail.CC & "; " & cCDORecipients(x).Name
        Case 3
            If x = 3 Then oReturnMail.BCC = ""
            oReturnMail.BCC = oReturnMail.BCC & "; " & cCDORecipients(x).Name
    End Select
Next

Set fn_outlook_select_names = oReturnMail

exit_function:
    On Error Resume Next
    'Housekeeping
    oSession.Logoff
    Set cCDORecipients = Nothing
    Set oRecipient = Nothing
    Set oCDORecipient = Nothing
    Set cRecipients = Nothing
    Set oSession = Nothing
    Exit Function

error_handler:
    'Trap this
    Call sb_error_handler(Err, "sb_outlook_select_names")
    Resume exit_function

End Function
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question