+ Reply to Thread
Results 1 to 9 of 9

Send Multiple Mail with Excel VBA

  1. #1
    Registered User
    Join Date
    10-20-2023
    Location
    Dubai
    MS-Off Ver
    365
    Posts
    5

    Thumbs up Send Multiple Mail with Excel VBA

    Thank you to all who proactively supported in getting this error answered.


    ORIGINAL SCRIPT

    Option Explicit

    Sub SendMultipleEMails()

    Dim emailApplication As Object
    Dim emailItem As Object
    Dim lastRow As Long
    Dim rng As Range
    Dim cell As Range


    '''Set the RANGE to run'''
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Range("A1:F" & lastRow)


    ''' THE EMAIL TO OUTLOOK '''
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)

    With emailItem

    .To = Cells(i, 1).Value
    .CC = Cells(i, 2).Value
    .Subject = Cells(i, 3).Value
    .Body = Cells(i, 4).Value
    ''.Attachments.Add Cells(i, 5).Value

    .Display

    End With

    On Error GoTo 0

    Set emailItem = Nothing
    Set emailApplication = Nothing


    End Sub




    SOLVED SCRIPT (after consolidating the answers)

    Sub SendMultipleEMails()

    Dim emailApplication As Object
    Dim emailItem As Object
    Dim lastRow As Long
    Dim i As Integer

    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Set emailApplication = CreateObject("Outlook.Application")

    For i = 2 To lastRow
    Set emailItem = emailApplication.CreateItem(0)

    With emailItem

    .To = Cells(i, 1).Value
    .CC = Cells(i, 2).Value
    .Subject = Cells(i, 3).Value
    .Body = Cells(i, 4).Value

    ''' ADDING ATTACHMENT '''
    If Cells(i, 5).Text <> "" Then
    .Attachments.Add Cells(i, 5).Text
    End If

    If Cells(i, 6).Text <> "" Then
    .Attachments.Add Cells(i, 6).Text
    End If

    If Cells(i, 7).Text <> "" Then
    .Attachments.Add Cells(i, 7).Text
    End If

    .Display

    End With

    On Error GoTo 0

    Set emailItem = Nothing
    Next i
    Set emailApplication = Nothing


    End Sub
    Last edited by RodPosadas; 12-22-2023 at 08:47 AM. Reason: solved

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Send Multiple Mail with Excel VBA

    I think you just need to add a loop from 1 to lastrow.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-20-2023
    Location
    Dubai
    MS-Off Ver
    365
    Posts
    5

    Re: Send Multiple Mail with Excel VBA

    Thank you for all the proactive support.
    Last edited by RodPosadas; 12-22-2023 at 08:48 AM. Reason: delete

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Send Multiple Mail with Excel VBA

    try adding this under Dim lastRow as long
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-13-2023
    Location
    Mansfield, Ohio
    MS-Off Ver
    2019
    Posts
    12

    Re: Send Multiple Mail with Excel VBA

    Try defining the variable at the top with the others
    Please Login or Register  to view this content.
    It's because you have Option Explicit in your code (at the very top before the Subs). This means that every variable has to be defined before it can be used. You don't have to define it at the top of each Sub, but you do have to define it before you use it.

    ByteMarks' answer above does a better job of handling larger quantities as Long goes to 2,147,483,647 and Integer only goes to 32,767, so it depends on the number of rows/column/cells you're working with.
    Last edited by mcsythera; 12-22-2023 at 08:31 AM. Reason: Better answer above.

  6. #6
    Registered User
    Join Date
    10-20-2023
    Location
    Dubai
    MS-Off Ver
    365
    Posts
    5

    Re: Send Multiple Mail with Excel VBA

    yup Dim i as integer worked

  7. #7
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Send Multiple Mail with Excel VBA

    Your variable lastRow is a long, so i should ideally be the same data type.

  8. #8
    Registered User
    Join Date
    12-13-2023
    Location
    Mansfield, Ohio
    MS-Off Ver
    2019
    Posts
    12

    Re: Send Multiple Mail with Excel VBA

    Please Login or Register  to view this content.
    should be

    Please Login or Register  to view this content.
    Note the lack of the "=".

  9. #9
    Registered User
    Join Date
    10-20-2023
    Location
    Dubai
    MS-Off Ver
    365
    Posts
    5

    Re: Send Multiple Mail with Excel VBA

    It works now. I can even add attachments.

    Sub SendMultipleEMails()

    Dim emailApplication As Object
    Dim emailItem As Object
    Dim lastRow As Long
    Dim i As Integer

    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Set emailApplication = CreateObject("Outlook.Application")

    For i = 2 To lastRow
    Set emailItem = emailApplication.CreateItem(0)

    With emailItem

    .To = Cells(i, 1).Value
    .CC = Cells(i, 2).Value
    .Subject = Cells(i, 3).Value
    .Body = Cells(i, 4).Value

    ''' ADDING ATTACHMENT '''
    If Cells(i, 5).Text <> "" Then
    .Attachments.Add Cells(i, 5).Text
    End If

    If Cells(i, 6).Text <> "" Then
    .Attachments.Add Cells(i, 6).Text
    End If

    If Cells(i, 7).Text <> "" Then
    .Attachments.Add Cells(i, 7).Text
    End If

    .Display

    End With

    On Error GoTo 0

    Set emailItem = Nothing
    Next i
    Set emailApplication = Nothing


    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Excel can send mail with multiple cells info as email body and subject, befr 3 day of due
    By ExcelUser2707 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-19-2013, 06:49 AM
  2. [SOLVED] Excel can send mail with multiple cells info as email body and subject, befr 3 day of due
    By ExcelUser2707 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 04:29 AM
  3. Help on Excel Macro/VBA Function to assign code to send outlook mail to multiple Receipt's
    By breadwinner in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 04-25-2013, 06:28 AM
  4. Replies: 0
    Last Post: 04-24-2013, 12:49 PM
  5. Automatic send email (without even click send in mail software) with excel vba ??
    By alexnkc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2013, 08:31 PM
  6. How to e-mail selected row and use e-mail address in a cell to send e-mail from excel
    By syedalamgir in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-27-2010, 02:15 AM
  7. [SOLVED] send mail from excel 2000 with multiple attachments
    By ...Patrick in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2006, 07:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1