Te la macro actualizada
Sub Enviar_Ficheros()
'Por Dante Amor
' Macro para generar ficheros y envío de correos
'
Application.ScreenUpdating = False
Dim h1 As Object, h2 As Object, h3 As Object
Dim rng As Range
Set h1 = Sheets("Listado")
Set h2 = Sheets("Formato")
Set h3 = Sheets("Temp")
'
ruta = ThisWorkbook.Path & "\"
fecha = Format(Date, "yyyymmdd")
h3.Cells.Clear
'
If h1.AutoFilterMode Then h1.AutoFilterMode = False
u1 = h1.Range("D" & Rows.Count).End(xlUp).Row
h1.Range("D5:G" & u1).Copy h3.Range("A1")
u3 = h3.Range("A" & Rows.Count).End(xlUp).Row
h3.Range("A1:D" & u3).RemoveDuplicates Columns:=1, Header:=xlYes
u3 = h3.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To u3
nombre = h3.Cells(i, "A").Value
correo = h3.Cells(i, "B").Value
ccopia = h3.Cells(i, "D").Value
'
h2.Rows("11:" & Rows.Count).ClearContents
If h1.AutoFilterMode Then h1.AutoFilterMode = False
h1.Range("A5:M" & u1).AutoFilter Field:=4, Criteria1:=nombre
u11 = h1.Range("D" & Rows.Count).End(xlUp).Row
h1.Range("A6:M" & u11).Copy
h2.Range("B11").PasteSpecial xlValues
'
'Generar Pdf
archivo = ruta & fecha & " Pedidos " & nombre & ".pdf"
h2.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=archivo, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
'
'Enviar correo
Set rng = h2.Range("A1:O49")
Set dam = CreateObject("Outlook.Application").CreateItem(0)
dam.To = correo 'Destinatarios
dam.Cc = ccopia 'Con copia
dam.Subject = "Valoración de Pedidos" '"Asunto"
dam.HTMLBody = RangetoHTML(rng)
dam.Attachments.Add archivo
'dam. Send 'El correo se envía en automático
dam. Display 'El correo se muestra
Next
If h1.AutoFilterMode Then h1.AutoFilterMode = False
h2.Rows("11:" & Rows.Count).ClearContents
Application.CutCopyMode = False
End Sub
'
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
.
.