J'essaie d'exporter dans mon fichier local unplage Excel définie comme image (PNG) (elle est nommée: "Print_Area" dans l'onglet "Summary" Range: P1: AI92). Le programme fonctionne bien, mais lorsque j'ouvre le fichier, toutes les images sont vides Voici le codage que j'utilise:
Sub _Daily_Mail()
Dim Rango7 As Range
Dim Archivo As String
Dim Imagen As Chart
Dim Result As Boolean
Set Rango7 = Sheets("Summary").Range("P2:AI92") " Summary
Sheets("Summary").Select
With Rango7
.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set Imagen = Rango7.Parent.ChartObjects.Add(33, 39, .Width, .Height).Chart
End With
Imagen.Paste
Imagen.ChartArea.Border.LineStyle = 0
Imagen.ChartArea.Width = Imagen.ChartArea.Width * 3
Imagen.ChartArea.Height = Imagen.ChartArea.Height * 3
Imagen.export "C:UsersmelyDocumentsImagenes_POSInforme1.png", filtername:="PNG"
Imagen.Parent.Delete
Set Imagen = Nothing
Réponses:
1 pour la réponse № 1Dans Excel 2016, vous devez utiliser la commande .Activate avant l'opération de collage. Exemple:
Set rng = Range("A1:C1")
With rng
.CopyPicture xlPrinter, xlPicture
Set oChart = ActiveSheet.ChartObjects.Add(.Left, .Top, 1920, 1080)
oChart.Activate
With oChart.Chart
.ChartArea.Border.LineStyle = 0
.Paste
.Export Filename:="C:File.jpg", Filtername:="jpg"
.Parent.Delete
End With
End With
0 pour la réponse № 2
Je rencontre le même problème avec Office 2016. Il semble que ce soit un problème de calendrier. Lorsque vous créez l'objet graphique et que vous pouvez y coller. Si je parcours le code, il fonctionne comme prévu et génère mon image.
J'ai trouvé un correctif qui semble fonctionner: Pour une raison quelconque, la sélection de la forme parent du graphique avant d'appeler le collage corrige le problème.
Function CopyRangeToPNG(ByRef rngImage As Range) As String
Dim vFilePath As Variant
rngImage.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With rngImage.Parent.ChartObjects.Add( _
Left:=rngImage.Left, Top:=rngImage.Top, _
Width:=rngImage.Width + 2, Height:=rngImage.Height + 2)
With .Chart
.Parent.Select
.ChartArea.Format.Line.Visible = msoFalse
.Paste
With .Pictures(1)
.Left = .Left + 2
.Top = .Top + 2
End With
" export
.Export CStr(ThisWorkbook.Path & "ImageName.PNG")
End With
.Delete
End With
CopyRangeToPNG = ThisWorkbook.Path & "ImageName.PNG"
End Function