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)
CÓDIGO SIN NEXT NI MENSAJE "mensajes enviados" se envía correctamente
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


0