none
outmail .Send error RRS feed

  • Question

  • Sub BirthDay()
    
    Dim OutApp As Object
        Dim OutMail As Object
    Dim olConItems As Outlook.Items
    Dim olItem As Outlook.ContactItem
    Dim olApp As Outlook.Application
    Dim olFolder As Outlook.Folder
    Set olApp = Application
    Set olFolder = olApp.GetNamespace("MAPI").GetDefaultFolder(10)
    Set olConItems = olFolder.Items
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        Dim a As String
    For Each olItem In olConItems
        If TypeName(olItem) = "ContactItem" Then
         If olItem.FirstName <> "" Then
           a = a + olItem.Email1Address + ";"
      With OutMail
           
    
            .Subject = "This is the Subject line"
            .Body = "Hi there"
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
     .To = olItem.Email1Address
            .Send
            End With
        End If
            End If
      
    Next olItem
        
    
    End Sub
    

    hello everybody, this is my code I'm trying to send email to every contact on my list contacts, but the .send methode isn't working for me OI get an error, i get a message that .To element is deleted or placed 

    thank you

    Wednesday, April 13, 2016 3:55 PM

Answers

  • Hello Ayman,

    >> Set OutApp = CreateObject("Outlook.Application")

    There is no need to create a new Application instance in the code of Outlook VBA macro. Use the Application property available in the VBA editor out of the box. See Getting Started with VBA in Outlook 2010 for more information.

    >> .To = olItem.Email1Address

    In order to send a message Outlook needs to know whom to deliver it to. The Recipients property is used for such a task. It returns an instance of the Recipients class. The Recipients class contains all recipients of your message, i.e. instances of the Recipient class in Outlook.

    The Recipients collection provides you the Add method that adds new recipients to the collection. The method accepts a string which contains the name of a recipient or a full SMTP e-mail address and returns a new instance of the Recipient class. Please pay special attention to the fact that the recipient’s name must be valid: it should have a corresponding entry in your address book .The collection also provides you the ResolveAll method, which doesn’t accept any parameters and returns a Boolean value indicating whether all recipients in the collection were resolved or not (false – if one or more entries were not resolved, i.e. don’t have corresponding entries in the address book). The Recipient class also provides the Resolved property which can be used instead of the ResolveAll method of the Recipients class. Note, if you specified an SMTP e-mail address, you don’t need to have a “mapped” entry in the address book – one-off entry will be created.

    See How To: Create and send an Outlook message programmatically for more information.


    [custom.development]

    • Marked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    • Unmarked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    • Marked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    Wednesday, April 13, 2016 6:09 PM

All replies

  • Hello Ayman,

    >> Set OutApp = CreateObject("Outlook.Application")

    There is no need to create a new Application instance in the code of Outlook VBA macro. Use the Application property available in the VBA editor out of the box. See Getting Started with VBA in Outlook 2010 for more information.

    >> .To = olItem.Email1Address

    In order to send a message Outlook needs to know whom to deliver it to. The Recipients property is used for such a task. It returns an instance of the Recipients class. The Recipients class contains all recipients of your message, i.e. instances of the Recipient class in Outlook.

    The Recipients collection provides you the Add method that adds new recipients to the collection. The method accepts a string which contains the name of a recipient or a full SMTP e-mail address and returns a new instance of the Recipient class. Please pay special attention to the fact that the recipient’s name must be valid: it should have a corresponding entry in your address book .The collection also provides you the ResolveAll method, which doesn’t accept any parameters and returns a Boolean value indicating whether all recipients in the collection were resolved or not (false – if one or more entries were not resolved, i.e. don’t have corresponding entries in the address book). The Recipient class also provides the Resolved property which can be used instead of the ResolveAll method of the Recipients class. Note, if you specified an SMTP e-mail address, you don’t need to have a “mapped” entry in the address book – one-off entry will be created.

    See How To: Create and send an Outlook message programmatically for more information.


    [custom.development]

    • Marked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    • Unmarked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    • Marked as answer by ayman-at1000 Wednesday, April 13, 2016 8:14 PM
    Wednesday, April 13, 2016 6:09 PM
  • Thank you so much
    Wednesday, April 13, 2016 8:14 PM