Home > Blockchain >  How to run properly this loop to send massive emails?
How to run properly this loop to send massive emails?

Time:07-29

I want to send massive mails to the mail adresses that are saved on column G (from G:10) until the end of the column. Note that the body of the message and the subject is the same for every email to be sent. This is the code:

Sub Email_From_Excel_Basic()

    Dim emailApplication As Object

    Dim emailItem As Object

    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)


    Dim cell As Range
    Set myDataRng = Range("G10:G15")
    
    ' Arquitectura del mail.
    For Each cell In myDataRng
        emailItem.To = cell.Value
        emailItem.Subject = Range("H5").Value
        emailItem.Body = Range("J5").Value
        ' Accion ejecutoria de envio de mail
        emailItem.send
    Next cell
    ' Descomentar la siguiente linea si quieres ver el correo antes de enviarse.
    ' emailItem.Display

    Set emailItem = Nothing

    Set emailApplication = Nothing

    
End Sub

But when running the error 'the item has been moved or deleted' is shown and only is sent the message to the first mail of the g column, any idea?

CodePudding user response:

myDataRng is declared as a Range, so you need to change this,

Set myDataRng = Range("G10:G15").Value

to this.

Set myDataRng = Range("G10:G15")

CodePudding user response:

The answer is:

For Each cell In myDataRng
    Set emailItem = emailApplication.CreateItem(0)
    With emailItem
        .To = cell.Value
        .Subject = Range("H5").Value
        .Body = Range("J5").Value
        .send
        
    End With
    
Next
    

A With structure properly written solved the issue of massive sending to the mails in the G column

  • Related