Consulting

Results 1 to 11 of 11

Thread: Mailing with Excell vbscript help

  1. #1

    Mailing with Excell vbscript help

    Hi im new to vba.
    I have an Excel sheet, one column with emails and another with Subjects (for the emails). Im trying to send emails to the different recipients with different subjects e.g.:

    = |=====A======|===B===
    1 |mail1@mail.com | subject1
    2 |mail2@mail.com | subject2
    3 |mail3@mail.com | subject3

    I have the following code:

    [VBA]Set OutApp = CreateObject("Outlook.Application")
    For Count = 1 To 3 //Assuming there are only 3 rows. But how can i make it loop till the end of the rows?
    Set objMessage = OutApp.CreateItem(0)
    objMessage.Subject = "Mail to" & ActiveSheet.Cells(Count, 1)//e.g. Email to mail1@mail.com must have a subject "Mail to subject1"
    objMessage.Bcc = ActiveSheet.Cells(Count, 2) //Bcc is required
    objMessage.Body = "This is some sample message text."
    objMessage.Send
    Next[/VBA]

    But i'm getting an "Outlook does not recognize one or more names" error.

    Any help with this code?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Maybe the email addresses are wrong.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Hmm, no they aren't. I've double-triple checked them before i post here.
    The problem is with the code or mis-configuration.

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Might just be me but i don't see where you are setting the recipient to. I see Subject/Bcc/Body & send (but no one to send to. Maybe try this.

    [VBA]Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim subject_ As String
    Dim Bcc_ As String
    Dim body_ As String
    'Dim attach_ As String

    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")

    ' Loop through the rows
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)


    email_ = cell.Value 'this will need to be a valid email address (main recipient)
    Bcc_ = cell.Offset(0, 1).Value 'this will need to be a valid email address (it's not at the moment)
    subject_ = cell.Offset(0, 1).Value
    body_ = "Hello" & vbNewLine & "World"


    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem
    .To = email_
    .Bcc = Bcc_
    .Subject = subject_
    .Body = body_
    '.Display
    .Send
    End With

    Next
    End Sub[/VBA]

    Hope this helps
    Last edited by georgiboy; 02-24-2012 at 05:20 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    Wow thanks for your reply georgiboy, very informative.
    Im gonna try it soon, thanks a lot :tu:

  6. #6
    Damn, same error again . Maybe its some configuration with Outlook. Anyway thanks for your help.

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    your bcc is not a valid email address, does it work if you take out bcc?

    [VBA]Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim subject_ As String
    'Dim Bcc_ As String
    Dim body_ As String
    'Dim attach_ As String

    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")

    ' Loop through the rows
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)


    email_ = cell.Value 'this will need to be a valid email address (main recipient)
    'Bcc_ = cell.Offset(0, 1).Value 'this will need to be a valid email address (it's not at the moment)
    subject_ = cell.Offset(0, 1).Value
    body_ = "Hello" & vbNewLine & "World"


    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem
    .To = email_
    '.Bcc = Bcc_
    .Subject = subject_
    .Body = body_
    '.Display
    .Send
    End With

    Next
    End Sub [/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    In fact, i have to use ONLY Bcc not To. I made the following changes
    -Bcc_ = cell.Value
    -commented To_

    but still didnt work

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Does it fail on every email address if you add the error line?
    [VBA]Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim subject_ As String
    Dim Bcc_ As String
    Dim body_ As String

    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")

    ' Loop through the rows
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)

    On Error Resume Next

    Bcc_ = Trim(cell.Value)
    subject_ = cell.Offset(0, 1).Value
    body_ = "Hello" & vbNewLine & "World"

    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem
    .Bcc = Bcc_
    .Subject = subject_
    .Body = body_
    .Send
    End With

    Next
    End Sub[/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    Finally it worked! I did something wrong the first time i edited your version georgiboy. Thanks a lot much appreciated your help!

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    You can take out the .Send and replace it with .Display then you can see what it's trying to send.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •