none
Outlook 2007 Selected Contacts RRS feed

  • Question

  • The following code uses an email template to send an email as one email to the selected contacts...which all show up on the To:  field

    But I can only select contacts from the same folder or subfolder...so is there a way to adjust this so it recognizes the contacts I select from different folders and creates the one email to all of them in the To:  field?   Thanks much

    Public Sub SendToSelectedContacts()
    Dim Selection As Selection
    Dim strDynamicDL As String
    Dim obj As Object
    Set Selection = ActiveExplorer.Selection

    For Each obj In Selection

    If obj.Class = olContact Then
    strDynamicDL = strDynamicDL & ";" & obj.Email1Address
    Else
    strDynamicDL = strDynamicDL & ";" & obj.DLName
    End If
    Next


    Dim objMsg As mailItem
    'Set objMsg = Application.CreateItem(olMailItem)
    Set objMsg = Application.CreateItemFromTemplate("C:\Users\UserName\AppData\Roaming\Microsoft\Templates\E-mail Form.oft")

    objMsg.To = strDynamicDL

    objMsg.Display


    Set objMsg = Nothing
    Set obj = Nothing
    End Sub

    Saturday, March 8, 2014 7:19 PM

Answers

  • Hello Imseaz,

    Yes, you can use contacts from different folders to fill the TO, CC or BCC fields.

    The fact is that the selection object contains only items from the current folder.

    Set Selection = ActiveExplorer.Selection
    The Selection property of the Explorer class returns a Selection object that contains the item or  items that are selected in the explorer window.

    As a workaround, you can track the SelectionChange and BeforeFolderSwitch events of the Explorer class for recording the selected items (their EntryIDs) for using later. Also you can add all recipients manually using the Recipients collection (see the corresponding property of Outlook items). For example:

    Set myItem = Application.CreateItem(olMailItem)  
    Set myRecipient = myItem.Recipients.Add("Dan Wilson") 
    

    Saturday, March 8, 2014 8:01 PM
  • The existing code will not be working for the described scenario. You need to develop a sophisticated solution - record all selected contacts programmatically, the SelectionChange and BeforeFolderSwitch events of the Explorer class can be used for this. And when you are done, you can create a message and fill the recipients collection.
    Saturday, March 8, 2014 9:23 PM

All replies

  • Hello Imseaz,

    Yes, you can use contacts from different folders to fill the TO, CC or BCC fields.

    The fact is that the selection object contains only items from the current folder.

    Set Selection = ActiveExplorer.Selection
    The Selection property of the Explorer class returns a Selection object that contains the item or  items that are selected in the explorer window.

    As a workaround, you can track the SelectionChange and BeforeFolderSwitch events of the Explorer class for recording the selected items (their EntryIDs) for using later. Also you can add all recipients manually using the Recipients collection (see the corresponding property of Outlook items). For example:

    Set myItem = Application.CreateItem(olMailItem)  
    Set myRecipient = myItem.Recipients.Add("Dan Wilson") 
    

    Saturday, March 8, 2014 8:01 PM
  • Thanks for what you said....but as I repeat, I am not an expert in this area....so what should I specifically change to the code so it does it?   I really appreciate it as it's important to me on the business side etc.
    Saturday, March 8, 2014 8:35 PM
  • The existing code will not be working for the described scenario. You need to develop a sophisticated solution - record all selected contacts programmatically, the SelectionChange and BeforeFolderSwitch events of the Explorer class can be used for this. And when you are done, you can create a message and fill the recipients collection.
    Saturday, March 8, 2014 9:23 PM
  • I put the following in a new Class Module.....and how do I run it before I do an email and how does it put it in the email To field:

      Public Sub Initialize_handler()
     
     Set myOlExp = Application.ActiveExplorer
     MsgBox myOlExp.Selection.Count & " items selected."
     
    End Sub
     
     
     
    Private Sub myOlExp_SelectionChange()
     
     MsgBox myOlExp.Selection.Count & " items selected."
     
    End Sub
            

    Saturday, March 8, 2014 11:09 PM
  • Hi Imseaz,

    MSDN provides the following sample code:

    This VBA example creates a new mail message, uses the Add method to add 'Dan Wilson' as a To recipient, and displays the message. To run this example without errors, replace 'Dan Wilson' with a valid recipient name.

    Sub CreateStatusReportToBoss() 
     Dim myItem As Outlook.MailItem  
     Dim myRecipient As Outlook.Recipient  
     
     Set myItem = Application.CreateItem(olMailItem) 
     Set myRecipient = myItem.Recipients.Add("Dan Wilson")  
     myItem.Subject = "Status Report"  
     myItem.Display  
    End Sub
    

    You can pass the contact's name or email address to the Add method of the Recipients class. Actually, it can be a string representing the display name, the alias, or the full SMTP e-mail address of the recipient.

    Sunday, March 9, 2014 8:33 AM
  • Thanks but this is not what I need....I need to have the change to the code I showed above so I can select contacts from different folders....or show me how to use the Class Module I read from another area you gave me.
    Monday, March 10, 2014 2:58 PM
  • I think Eugene have given you a right direction, but you should try to change your code by yourself.
    Friday, March 14, 2014 10:18 AM