Problema con macro de envío de correos vba

Soy nuevo en el foro y necesito ayuda urgente. Resulta que tengo este código vba que enviaba correos desde un libro excel, para esto seleccionaba la hoja activa en relación a una lista de contactos y la enviaba por email. El problema es que el código utiliza el .hasroutingslip y este dejó de funcionar en la version 2007. Por más que he intentado, no encuentro como hacer un código que haga ahora lo mismo. Este es el código; por favor necesito ayuda urgente T_T

Private Sub CommandButton1_Click()

Dim strInstalador As String
Dim strEmail1 As String
Dim strEmail2 As String
Dim strEmail3 As String
Dim strEmail4 As String
Dim strEmail5 As String
Dim strEmail6 As String
Dim strEmail7 As String

ActiveWorkbook.Sheets("Emails").Select
ActiveWorkbook.Sheets("Emails").Range("A1").Select
Selection.End(xlDown).Select
intTotalInstaladores = ActiveCell.Row - 1
i = 1
Do
Workbooks("BD - CORREOS FINAL DRP.xls").Activate
strInstalador = ActiveWorkbook.Sheets("Emails").Range("A" & i + 1).Value
'Encontrar el Email del agente
ActiveWorkbook.Sheets("Emails").Select
ActiveWorkbook.Sheets("Emails").Cells.Select
Selection.Find(What:=strInstalador, After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
strEmail1 = ActiveCell.Offset(0, 1).Value
strEmail2 = ActiveCell.Offset(0, 2).Value
strEmail3 = ActiveCell.Offset(0, 3).Value
strEmail4 = ActiveCell.Offset(0, 4).Value
strEmail5 = ActiveCell.Offset(0, 5).Value
strEmail6 = ActiveCell.Offset(0, 6).Value
strEmail7 = ActiveCell.Offset(0, 7).Value

ActiveWorkbook.Sheets("BD").Select
ActiveWindow.LargeScroll ToRight:=1

Selection.AutoFilter Field:=1, Criteria1:=strInstalador

ActiveWorkbook.Sheets("BD").Range("a1").Select

ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste
ActiveSheet.Select
ActiveSheet.Cells.EntireColumn.AutoFit
ActiveSheet.Select
ActiveSheet.Move
ActiveSheet.Name = strInstalador

ActiveSheet.Select
ActiveSheet.Range("A2").Select
If ActiveCell.Value <> "" Then
ActiveSheet.Range("A1").Select
Selection.End(xlDown).Select
intTotalJobcards = ActiveCell.Row - 1
'Envío del email
With ActiveWorkbook
.HasRoutingSlip = True
With .RoutingSlip
.Delivery = xlAllAtOnce
.Recipients = Array(strEmail1, strEmail2, strEmail3, strEmail4, strEmail5, strEmail6, strEmail7)
.Subject = " " & strInstalador
.Message = .Message & " " & Chr(10)
.Message = .Message & " " & Chr(10)
.ReturnWhenDone = True
End With
.Route
End With
End If
ActiveWorkbook.Save
ActiveWorkbook.Close
i = i + 1
Loop Until i > intTotalInstaladores
End Sub

1 respuesta

Respuesta
1

 H o l a:

Si ya no te funciona esa instrucción, entonces, una opción es cambiar la función para enviar correos.

¿El correo lo estás enviando por outlook?

Explícame qué necesitas enviar y cambio la función a CreateObject("outlook.application")

'

Hola, gracias por tu tiempo para responder.

Sí, el correo lo envío por outlook.

Lo que hago inicialmente es seleccionar la hoja activa en base a una hoja del libro que contiene las direcciones electrónicas que esta en la primera parte del código (la parte hasta antes de "'Envio del email") y luego la enviaba como archivo adjunto a la dirección electrónica que correspondía. 
Respondiendo a tu pregunta, es eso... que me envíe la hoja activa por correo como adjunto a la dirección que corresponda.
saludos,

 H o l a :

¿Solamente me queda la duda si las 2 hojas "emails" y "bd" están en el mismo libro?

Te anexo la macro actualizada, prueba y me comentas.

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    ActiveWorkbook.Sheets("Emails").Select
    ActiveWorkbook.Sheets("Emails").Range("A1").Select
    Selection.End(xlDown).Select
    intTotalInstaladores = ActiveCell.Row - 1
    i = 1
    Do
        Workbooks("BD - CORREOS FINAL DRP.xls").Activate
        strInstalador = ActiveWorkbook.Sheets("Emails").Range("A" & i + 1).Value
        'Encontrar el Email del agente
        ActiveWorkbook.Sheets("Emails").Select
        ActiveWorkbook.Sheets("Emails").Cells.Select
        Selection.Find(What:=strInstalador, After:=ActiveCell, LookIn _
            :=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        strEmail1 = ActiveCell.Offset(0, 1).Value
        strEmail2 = ActiveCell.Offset(0, 2).Value
        strEmail3 = ActiveCell.Offset(0, 3).Value
        strEmail4 = ActiveCell.Offset(0, 4).Value
        strEmail5 = ActiveCell.Offset(0, 5).Value
        strEmail6 = ActiveCell.Offset(0, 6).Value
        strEmail7 = ActiveCell.Offset(0, 7).Value
        para = strEmail1 & ";" & strEmail2 & ";" & strEmail3 & ";" & _
               strEmail4 & ";" & strEmail5 & ";" & strEmail6 & ";" & strEmail7
        '
        ActiveWorkbook.Sheets("BD").Select
        Selection.AutoFilter Field:=1, Criteria1:=strInstalador
        ActiveWorkbook.Sheets("BD").Range("a1").Select
        ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlDown)).Select
        ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlToRight)).Select
        Selection. Copy
        Sheets. Add
        ActiveSheet. Paste
        ActiveSheet. Cells. EntireColumn. AutoFit
        ActiveSheet. Move
        ActiveSheet.Name = strInstalador
        archivo = ThisWorkbook.Path & "\" & strInstalador & ".xlsx"
        ActiveWorkbook.SaveAs archivo, _
            FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
        '
        If Range("A2").Value <> "" Then
            'Envío del email
            Set dam = CreateObject("outlook.application").createitem(0)
            dam.To = para
            dam.Subject = " " & strInstalador
            dam.Body = "" '"Cuerpo del mensaje"
            dam.Attachments.Add archivo
            dam.Send
        End If
        i = i + 1
    Loop Until i > intTotalInstaladores
End Sub

Cualquier duda avísame.

'

Oh, no me saltó la alerta de la respuesta. Voy a probar la macro y te escribo, nuevamente muchas gracias por tu ayuda. Respecto a tu pregunta, ambas hojas se encuentran en el mismo libro.

Lo reviso y te confirmo. Muchas gracias nuevamente.

De acuerdo, cualquier duda avísame, si todo funciona bien, recuerda valorar la respuesta.

Hola, que tal. Te comento que la macro no está enviando el correo, solo está generando un archivo excel para cada destinatario que ubica en la hoja del libro BD...

Solamente pon cualquier dato en la celda A2 y vuelve a probar

No he recibido comentarios, si te funciona la macro, recuerda valorar la respuesta.

Hola, disculpa por la demora. Te comento que aún cuando coloco un valor en la celda A2 de ambas hojas del libro, lo único que está haciendo la macro es crearme un archivo excel en la carpeta donde la ejecuto pero no envía el correo. 
Qué puedo hacer :S

Envíame tu archivo con la macro que te envié para revisarlo.

Mi correo [email protected]

En el asunto del correo escribe tu nombre de usuario “Jesús Gutierrez” y el título de esta pregunta.

Esta es la macro actualizada:

Private Sub CommandButton1_Click()
    Dim strInstalador As String
    Dim strEmail1 As String
    Dim strEmail2 As String
    Dim strEmail3 As String
    Dim strEmail4 As String
    Dim strEmail5 As String
    Dim strEmail6 As String
    Dim strEmail7 As String
    ActiveWorkbook.Sheets("Emails").Select
    ActiveWorkbook.Sheets("Emails").Range("A1").Select
    Selection.End(xlDown).Select
    intTotalInstaladores = ActiveCell.Row - 1
    i = 1
    Do
        'Workbooks("BD - CORREOS FINAL DRP.xlsm").Activate
        strInstalador = ActiveWorkbook.Sheets("Emails").Range("A" & i + 1).Value
            'Encontrar el Email del agente
        ActiveWorkbook.Sheets("Emails").Select
        ActiveWorkbook.Sheets("Emails").Cells.Select
        Selection.Find(What:=strInstalador, After:=ActiveCell, LookIn _
            :=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        strEmail1 = ActiveCell.Offset(0, 1).Value
        strEmail2 = ActiveCell.Offset(0, 2).Value
        strEmail3 = ActiveCell.Offset(0, 3).Value
        strEmail4 = ActiveCell.Offset(0, 4).Value
        strEmail5 = ActiveCell.Offset(0, 5).Value
        strEmail6 = ActiveCell.Offset(0, 6).Value
        strEmail7 = ActiveCell.Offset(0, 7).Value
        ActiveWorkbook.Sheets("BD").Select
        ActiveWindow.LargeScroll ToRight:=1
        Selection.AutoFilter Field:=1, Criteria1:=strInstalador
        ActiveWorkbook.Sheets("BD").Range("a1").Select
        ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlDown)).Select
        ActiveWorkbook.Sheets("BD").Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
        Sheets.Add
        ActiveSheet.Paste
        ActiveSheet.Select
        ActiveSheet.Cells.EntireColumn.AutoFit
        ActiveSheet.Select
        ActiveSheet.Move
        ActiveSheet.Name = strInstalador
        ActiveSheet.Select
        ActiveSheet.Range("A2").Select
        If ActiveCell.Value <> "" Then
            ActiveSheet.Range("A1").Select
            Selection.End(xlDown).Select
            intTotalJobcards = ActiveCell.Row - 1
            para = strEmail1 & ";" & strEmail2 & ";" & strEmail3 & ";" & _
                   strEmail4 & ";" & strEmail5 & ";" & strEmail6 & ";" & strEmail7
            archivo = ThisWorkbook.Path & "\" & strInstalador & ".xlsx"
            ActiveWorkbook.SaveAs archivo, _
                    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
            'Envío del email
            Set dam = CreateObject("outlook.application").createitem(0)
            dam.To = para
            dam.Subject = " " & strInstalador
            dam.Body = "" '"Cuerpo del mensaje"
            dam.Attachments.Add archivo
            dam.Display
        End If
        i = i + 1
   Loop Until i > intTotalInstaladores
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas