Excel - Necesito ayuda para rematar el código de una macro (error 91)

 
Vista:

Necesito ayuda para rematar el código de una macro (error 91)

Publicado por MaríaE (3 intervenciones) el 09/01/2019 10:27:29
Hola a todos;

Aquí os paso dos códigos para ver si me podéis ayudar. He copiado un código para enviar mails a través de gmail, y quiero que al finalizar el envío de los mails según el rango que indico, me salte un mensaje "correos enviados". En la primera macro añado para que me salte correos enviados y me da error 91. En la segunda no meto este apartado de next final para que me salga el mensaje y no me da error. ¿Podríais ayudarme?
Muchas gracias

CODIGO NEXT Y MENSAJE "CORREOS ENVIADOS" ME DA ERROR
(me da error 91 en tiempo de ejecución: Variable de objetivo o bloque with no establecido)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Sub enviarmail()
Set MiCorreo = New CDO.Message
        '
        With MiCorreo.Configuration.Fields
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "XXX"
            .Update
        End With
    '
   '
   For Each Cell In ThisWorkbook.Sheets("Hoja1").Range("B2:B4")
        '
 
        '
        Asunto = "xxx"
        Destinatario = Cell.Offset(0, -1).Value
        Correo = Cell.Value
        Link = Cell.Offset(0, 1).Value
        Link = Cell.Offset(0, 2).Value
 
        'Cuerpo del mensaje
        '
        Msg = "Estimado " & Destinatario & vbNewLine & vbNewLine
        Msg = Msg & "¡xxx" & vbNewLine
        Msg = Msg & Link & "." & vbNewLine & vbNewLine
        Msg = Msg & "xxx" & vbNewLine
        Msg = Msg & Link & "." & vbNewLine & vbNewLine
        Msg = Msg & "xxx " & vbNewLine & vbNewLine
        Msg = Msg & "xxx " & vbNewLine & vbNewLine
        Msg = Msg & "xxx"
        '
        With MiCorreo
            .Subject = Asunto
            .From = "email"
            .To = Correo
            .TextBody = Msg
 
        End With
        '
        MiCorreo.Send
Set MiCorreo = Nothing
    '
'
Next Cell
 
MsgBox "Correos enviados", vbInformation, "EXCELeINFO"
 
End Sub



CÓDIGO SIN NEXT NI MENSAJE "mensajes enviados" se envía correctamente

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Sub enviarmail()
Set MiCorreo = New CDO.Message
        '
        With MiCorreo.Configuration.Fields
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "direccionemail"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
            .Update
        End With
    '
 
    '
   For Each Cell In ThisWorkbook.Sheets("Hoja1").Range("B2:B9")
 
        '
        Asunto = "XXX"
        Destinatario = Cell.Offset(0, -1).Value
        Correo = Cell.Value
        Link = Cell.Offset(0, 1).Value
        Link = Cell.Offset(0, 2).Value
 
        'Cuerpo del mensaje
        '
        Msg = "Estimado " & Destinatario & vbNewLine & vbNewLine
        Msg = Msg & "XXX" & vbNewLine
        Msg = Msg & Link & "." & vbNewLine & vbNewLine
        Msg = Msg & "XXX" & vbNewLine
        Msg = Msg & LinkVendedor & "." & vbNewLine & vbNewLine
        Msg = Msg & "XXX" & vbNewLine & vbNewLine
        Msg = Msg & "XXX " & vbNewLine & vbNewLine
        Msg = Msg & "XXX"
        '
        With MiCorreo
            .Subject = Asunto
            .From = "direccion"
            .To = Correo
            .TextBody = Msg
 
        End With
        '
        MiCorreo.Send
 
Next
 
 
End Sub
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Necesito ayuda para rematar el código de una macro (error 91)

Publicado por Antoni Masana (2473 intervenciones) el 09/01/2019 12:53:59
El responsable de tu dolor de cabeza es esta línea dentro del FOR

1
Set MiCorreo = Nothing

Ponla después del NEXT y antes o después del MsgBox.

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

Necesito ayuda para rematar el código de una macro (error 91)

Publicado por MariaE (3 intervenciones) el 09/01/2019 16:46:13
Muchas gracias Antoni! He metido lo que indico abajo después de la línea 45 "MiCorreo.Send" y me sale el siguiente error: Se ha producido el error '-2147220979 (8004020d)' en tiempo de ejecución: error de Automatización, sabes por qué puede ser?

Gracias de nuevo!



Next Cell

Set MiCorreo = Nothing

MsgBox "Correos enviados", vbInformation, "EXCELeINFO"

Set MiCorreo = Nothing

End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Necesito ayuda para rematar el código de una macro (error 91)

Publicado por Antoni Masana (2473 intervenciones) el 09/01/2019 21:26:16
Habría que ejecutar la macro paso a paso para ver donde da el error y que valores tienen las variables y los objetos.

las ultimas lineas deberían ser:

1
2
3
4
5
6
7
8
9
Sub enviarmail()
    Set MiCorreo = New CDO.Message
    ...
        End With
        MiCorreo.Send
    Next Cell
    Set MiCorreo = Nothing
    MsgBox "Correos enviados", vbInformation, "EXCELeINFO"
End Sub


Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Necesito ayuda para rematar el código de una macro (error 91)

Publicado por Eugenia (3 intervenciones) el 10/01/2019 10:58:51
Funcionó!

Muchísimas gracias Antoni!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar