1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

VB script to send email using outlook contact distribution list

Discussion in 'Visual Basic ( VB )' started by bulleye, Oct 12, 2006.

  1. bulleye

    bulleye New Member

    Hi everyone,
    i am new here and my first post. i'd like to send email from excel using vbcript. how i code it so that the email are sent to the distribution list in my outlook 2003 contacts. below is the codes for sending mail to indivual email addresses. Any help would be appriciated:
    Sub Senmail()
    Dim objOutlook As Object
    Dim objOutlookMsg As Object
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(0)
    With objOutlookMsg
       .To = "name@email.com"
       .Cc = "name@email.com"
       .Subject = "Hello World (one more time)..."
       .Body = "This is the body of message"
       .HTMLBody = "HTML version of message"
       .Attachments.Add ("f:\Test.txt")
       .Send 'Let´s go!
    End With
    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing
    End Sub
    Last edited by a moderator: Oct 13, 2006
  2. pradeep

    pradeep Team Leader

    Ee have to look at each and every item in the Contacts folder and check to see if the item happens to be a distribution list. If it is, we can then bind to that item (the distribution list) and methodically collect all the members and send them a mail.

    Const olFolderContacts = 10
      sDistName = "Friends" 'set the distribution list name here
      Set objOutlook = CreateObject("Outlook.Application")
      Set objNamespace = objOutlook.GetNamespace("MAPI")
      Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items
      intCount = colContacts.Count
      For i = 1 To intCount
          If TypeName(colContacts.Item(i)) = "DistListItem" Then
              Set objDistList = colContacts.Item(i)
              sEmails = "";
              If objDistList.DLName = sDistName Then
                  For j = 1 To objDistList.MemberCount
                      sEmails = sEmails & ";" & objDistList.GetMember(j).Address
                  'send the mail using the emails u got! ;-) enjoy
              End if
          End If
  3. bulleye

    bulleye New Member

    Thanks so much for the reply Pradeep. Please excuse my VB ignorance but how can I intergrate your codes to make it work. I added your codes to my and it didn't compile.
  4. smileyshowers

    smileyshowers New Member

    could you please tell me what to do to display the body of the mail line by line. and also if we want to display multiple number of items using a forloop..what to do?? thanks for your code for how to send mail from vb
  5. pradeep

    pradeep Team Leader

    I didnt exactly egt what you want to do, please explain.
  6. smileyshowers

    smileyshowers New Member

    When we are sending mail from VB script,
    we specify,
    with <object>
    .To ,

    in this, in body, we can enter only text. What I am asking is, I want to get the data in the body of mail line by line. To get line by line, is there any property to be specified in .body section? Also I'm having Array of values. I want to display them in the mail in the body section. How can we do that?

    Hope you got my question...

  7. pradeep

    pradeep Team Leader

    The HTMLBody is optional, you can only specify the text body if you want. To print an array of values, concatenate each array value into a string and then put it in the mail body. And btw, I still didnt get what you mean by want to get the data in the body of mail line by line.
  8. smileyshowers

    smileyshowers New Member

    I want the text of the body to be displayed as line by line:
    "This is my name.
    I am doing job.
    I am working."
    instead of
    "This is my name.I am doing job. I am working." (This is concatenation)
  9. pradeep

    pradeep Team Leader

    strA = "This is my name."
    strB = "I am doing job."
    strC = strA & vbCrLf & strB;
    This gives:
    This is my name.
    I am doing job.
  10. smileyshowers

    smileyshowers New Member

    Thanks pradeep.. One more query............
    When i use the above code to send mail, after the mail gets triggered from VB, a dialogue box occurs on the desktop asking "Yes","No","Cancel". is there any code that automatically "Yes" must get clicked without user interaction. Can you suggest any solution for that.
  11. pradeep

    pradeep Team Leader

    It doesn't ask for confirmation by default, there must be some code which triggers the confirmation dialog box.
  12. d3w11

    d3w11 New Member

    I am also getting this box. This pop up comes from Outlook saying:

    "A program is trying to automatically send e-mail on your behalf. Do you want to allow this? If this is unexpected, it may be a virus and you should choose no."

    THen there's a yes and no button for you to choose.

    Without choosing this button, the email will not run.

    Has anyone figured out how to get around this message? Any help will be appreciated.
  13. d3w11

    d3w11 New Member

    Just figured out that this pop up only occurs in 2003.

    I tried it in 2002, and no box came up.

    If anyone knows of a solution please let me know.

    Thank you!
  14. kawior

    kawior New Member

    Hi everyone,

    I am new to this subject. I managed to use the code from the first post and it works fine for me.
    However when I send an email manually I have Word as an editor for emails and I am able to insert all kinds of formatting, for example Bold font or bullets for numbering items in a list. Can I insert formatting in .body or HTMLBody? Can you advise how to achieve this?

  15. pradeep

    pradeep Team Leader

    If you want formatting in your content, you'll need use HTML, and also send it as HTML body!
  16. MikD

    MikD New Member


    I found this while looking for something else....

    The box you are seeing is due to the enhanced security in Outlook 2003 and can't be disabled. There are soloutions out there like ExpressClickYes, but personally I opted to look at Redemption (http://www.dimastr.com/redemption/) This allows you to access outlook and bypass the enhanced security.

    Hope this helps! :)

Share This Page