Estimados tengo el siguiente problema, estoy intentendo insertar como imagen una tabla dimanica desde un excel a un correo en outlook, el tema es que no me inserta la tabla dinamica, sino un grafico.
>>>>>> este es el codigo que uso<<<<<<
Sub AVISO_DE_PAGO()
Dim OutApp As Object, OutMail As Object
For x = 1 To ThisWorkbook.Sheets.Count
If Sheets(x).Name <> "DATOS" And Sheets(x).Name <> "INSTRUCCION" And Sheets(x).Name <> "Detalle de Pago" And Sheets(x).Name <> "CONTACTOS" Then
Sheets(x).Select
On Error Resume Next
Sheets(x).Range("A1", Range("H50").End(xlUp)).Select
Selection.Copy
With ActiveSheet
.Pictures.Paste
.Shapes(ActiveSheet.Shapes.Count).Cut
>>>>>'este es el codigo que causa problemas <<<<<<
.ChartObjects.Add(1, 1, Selection.Width, Selection.Height).Select
'>><<
End With
With ActiveChart
.Paste
.Export Filename:=ThisWorkbook.Path & "\Planilla.jpg", FilterName:="JPG"
.Parent.Delete
End With
Set OutApp = CreateObject("Outlook.Application") 'New Outlook.Application
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Sheets(x).Range("N1").Value
.CC = Sheets(x).Range("O1") & ";" & Sheets(x).Range("O2") & ";" & Sheets(x).Range("O3") & ";" & Sheets(x).Range("O4") & ";" & Sheets(x).Range("O2")
If Month(Sheets("INSTRUCCION").Range("D2")) < 10 Then
.Subject = "Aviso de pago " & Sheets(x).Range("A4") & " " & "0" & Month(Sheets("INSTRUCCION").Range("D2")) & "-" & Year(Sheets("INSTRUCCION").Range("D2"))
Else
.Subject = "Aviso de pago " & Sheets(x).Range("A4") & " " & Month(Sheets("INSTRUCCION").Range("D2")) & "-" & Year(Sheets("INSTRUCCION").Range("D2"))
End If
.Attachments.Add ThisWorkbook.Path & "\Planilla.jpg"
.BodyFormat = 2 'olFormatHTML
If Application.WorksheetFunction.VLookup(Range("B1") & "Para", Sheets("CONTACTOS").Range("A:F"), 6, 0) = 1 Then
.HTMLBody = "<span style = "" color: #1F497D ""></ span style = "" color: #1F497D "">" _
& "<p style = 'font-family: arial; font-size: 13'>" _
& "Estimado " & Sheets(x).Range("M1") & "<br />" & "<br />" _
& "Hemos instruido el proceso de pago para las facturas de acuerdo al siguiente detalle:<br> " _
& "<br>" _
& "<IMG SRC='Planilla.jpg'>"
Else
.HTMLBody = "<span style = "" color: #1F497D ""></ span style = "" color: #1F497D "">" _
& "<p style = 'font-family: arial; font-size: 13'>" _
& "Estimada " & Sheets(x).Range("M1") & "<br />" & "<br />" _
& "Hemos instruido el proceso de pago para las facturas de acuerdo al siguiente detalle:<br> " _
& "<br>" _
& "<IMG SRC='Planilla.jpg'>"
End If
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next x
End Sub
Parece que Excel trata de crearun gráfico dinámico, porque tienes una selección que es una tabla dinámica.
Como tantas otras veces, seguramente con la mejor intención, Excel se extralimita haciendo algo que no se le ha pedido.
La solución es no tener nada seleccionado cuando se crea el gráfico.
Así que podrías por ejemplo primero crear el gráfico, y luego seleccionar el rango con el que quieres crear la imagen que vas a pegar dentro de ese gráfico.
podría quedar algo así:
Sub test2()
Dim miGrafico As Chart
With ActiveSheet
'primero creamos un gráfico sin nada seleccionado
.Range("Z1").Select 'selecciono una celda fuera de la tabla dinámica
Set miGrafico = .ChartObjects.Add(1000, 1, Selection.Width, Selection.Height).Chart
'ahora copio el rango que me interesa
.Range("A1", Range("H50").End(xlUp)).Select
Selection.Copy
.Pictures.Paste
.Shapes(ActiveSheet.Shapes.Count).Cut
End With
With miGrafico
' ajusto alto y ancho
.Parent.Width = Selection.Width
.Parent.Height = Selection.Height
' pego la imagen
.Paste
' guardamos como archivo de imagen
.Export Filename:=ThisWorkbook.Path & "\Planilla.jpg", FilterName:="JPG"
' borramos el gráfico
.Parent.Delete
End With
End Sub
------
Ya sé Excel, pero necesito más.