none
Adding recipients to mail item using SelectNamesDialog RRS feed

  • Question

  • Hello there, 

    I have been developing some VBA to forward an Email with a prepopulated message to a specified user using the SelectNamesDialog.

    My current code is as follows,

    Sub ForwardItem()
    Dim oExplorer As Outlook.Explorer
    Dim oMail As Outlook.MailItem
    Dim oOldMail As Outlook.MailItem
    Dim oDialog As SelectNamesDialog


    Set oExplorer = Application.ActiveExplorer
    If oExplorer.Selection.Item(1).Class = olMail Then
    Set oOldMail = oExplorer.Selection.Item(1)
    Set oMail = oOldMail.Forward


    Set oDialog = Application.Session.GetSelectNamesDialog

        With oDialog
            .InitialAddressList = Application.Session.AddressLists("Global Address List")
            .ShowOnlyInitialAddressList = False
            If .Display Then
            End If
        End With

    oMail.Recipients.Add (oDialog.Recipients)
    oMail.Recipients.Item(1).Resolve
    If oMail.Recipients.Item(1).Resolved Then
    oMail.Body = "Hi " + oMail.Recipients.Item(1).Name + "," & vbCrLf & vbCrLf & "I have updated this ticket with the following from " + oOldMail.Sender + ". " & vbCrLf & vbCrLf & "Kind regards," & oMail.Body
    oMail.Display

    Else
    MsgBox "Could not resolve recipient name" & oMail.Recipients.Item(1).Name
    End If
    Else
    MsgBox "Not a mail item"
    End If
    End Sub

    The problem I'm having is adding the selected recipient from the Dialogue box to "oMail.Recipients.Add ()"

    Any assistance would be appreciated.

    Kind regards,

    Sam (struggling VBA user)

    Friday, May 16, 2014 3:39 PM

Answers

  • Sam,

    > How can i then add the selected recipient within the SelectNamesDialog to the "oMail" Recipients.

    If you have only one recipient selected you can use the following code:

    oMail.Recipients.Add (oDialog.Recipients(1).Address)

    or

    oMail.Recipients.Add (oDialog.Recipients(1).Name)

    In case if you expect multiple recipients, you need to iterate over all collection items and add them one-by-one. The Outlook object model doesn't provide any property or method for adding them at once.

    • Marked as answer by SamPhillips Monday, May 19, 2014 3:57 PM
    Friday, May 16, 2014 6:12 PM

All replies

  • Hello Sam,

    The Add method of the Recipients class expects a string which represents the name of the recipient; it can be a string representing the display name, the alias, or the full SMTP e-mail address of the recipient.

    Friday, May 16, 2014 3:55 PM
  • Thank you,

    How can i then add the selected recipient within the SelectNamesDialog to the "oMail" Recipients.

    My trail of thought is in the below code:

    oMail.Recipients.Add (oDialog.Recipients)

    Is there a Get command i can place inside the brackets that returns the required string value?

    Thank you.

    Friday, May 16, 2014 4:02 PM
  • You would need to loop through all recipients and add them one at a time:

    for each oRecip in oDialog.Recipients
      set oNewRecip = oMail.Recipients.Add(oRecip.Address)
      oNewRecip.Type = oRecip.Type
    next
    If using Redemption is an option, it allows the RDOMail.Recipients property to be set to an instance of the RDORecipients collection, such as that returned by the RDOSession.AddressBook.ShowAddressBook. In the Outlook Object Model, MailItem.Recipients property is read-only.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Friday, May 16, 2014 4:22 PM
  • Sam,

    > How can i then add the selected recipient within the SelectNamesDialog to the "oMail" Recipients.

    If you have only one recipient selected you can use the following code:

    oMail.Recipients.Add (oDialog.Recipients(1).Address)

    or

    oMail.Recipients.Add (oDialog.Recipients(1).Name)

    In case if you expect multiple recipients, you need to iterate over all collection items and add them one-by-one. The Outlook object model doesn't provide any property or method for adding them at once.

    • Marked as answer by SamPhillips Monday, May 19, 2014 3:57 PM
    Friday, May 16, 2014 6:12 PM