Validate if you find an attachment VBA macro gmail

1

I have this macro that sends several emails at the same time with different attachments to X number of previously specified emails.

Always the emails must have an attachment (this is the reason for the email), I am trying that when I do not find the attachment I show an error and automatically jump to send the other mail, but it is the moment that I have not found the way to do it.

Public impresiones As Integer
Sub SendMail_Gmail2()
  Dim myrange As Range
  Dim contador As Integer
  Set myrange = Worksheets("Hoja1").Range("A:A")
  contador = Application.WorksheetFunction.CountA(myrange)
  contador = 4
  For i = 2 To contador
    Dim Email As CDO.Message
    Set Email = New CDO.Message
    correo = ""
    passwd = "12345678"
    destino1 = Range("D" & i)
    destino2 = Range("E" & i)
    destino3 = Range("F" & i)
    destino4 = Range("G" & i)
    destino5 = Range("H" & i)
    destino6 = Range("I" & i)
    cliente = Range("B" & i)
    mensaje = "TITULO"
    cuerpo = "CONTENIDO"
    archivo = Range("K" & i)
    Email.Configuration.Fields(cdoSMTPServer) = "smtp.gmail.com"
    Email.Configuration.Fields(cdoSendUsingMethod) = 2
    With Email.Configuration.Fields
      .Item("...") = CLng(465)
      .Item("...") = Abs(1)
      .Item("...") = 30
      .Item("...") = correo
      .Item("...") = passwd
      .Item("...") = True
    End With
    With Email
      .To = destino1 & ", " & destino2 & ", " & destino3 & ", " & destino4 & ", " & destino5 & ", " & destino6
      .From = correo
      .Subject = mensaje
      .TextBody = cuerpo
      .AddAttachment archivo
      .Configuration.Fields.Update
      On Error Resume Next
      'If .Attachments.Item(Index) Is Nothing Then
      'MsgBox "no hay adjunto para: " & cliente
      'End If
      .Send
    End With
    If Err.Number = 0 Then
      MsgBox "El mail se envió con éxito a: " & cliente, vbInformation, "Mensaje Administrador"
      impresiones = impresiones + 1
    Else
      MsgBox "Se produjo el siguiente error: " & Err.Description, vbCritical, "Error nro " & Err.Number
    End If
  Next
  MsgBox "Se han enviado " & impresiones & " E-Mails correctamente"
  impresiones = 0
End Sub
    
asked by Víctor Trujillo 26.07.2017 в 17:15
source

1 answer

2

As far as I can see, you only send an email with several recipients, you could create a loop that sends the messages one at a time and in each one the verification

    If .Attachments.Item(Index) Is Nothing Then
      'MsgBox "no hay adjunto para: " & cliente
      'End If

because for now with a destination has attached no longer would be in this condition

    
answered by 27.07.2017 в 10:55