Save Graphics as PNG
To save all charts use this code:
For Each sht In ActiveWorkbook.Sheets
x = 1
For Each co In sht.ChartObjects
co.Chart.Export SeuDiretório & "\" & sht.Name _
& "_" & x & ".png", "PNG"
x = x + 1
Next co
Next sht
Complete Code
A more complete code to save all charts in the same file folder and in case you don’t find the file, open a window to choose the directory path:
Sub ExportarGrafico()
Dim strPath As String
Dim co As ChartObject
Dim x As Long
Dim sht As Worksheet
strPath = ThisWorkbook.Path
Inicio:
If strPath <> "" Then
For Each sht In ThisWorkbook.Sheets
x = 1
For Each co In sht.ChartObjects
co.Chart.Export strPath & "\" & sht.Name _
& "_" & x & ".png", "PNG"
x = x + 1
Next co
Next sht
Else
MsgBox "A pasta do arquivo não foi encontrada - Escolha a pasta."
strPath = GetFolder
GoTo Inicio
End If
End Sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select folder to export Charts to"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show = True Then sItem = .SelectedItems(1)
End With
GetFolder = sItem
Set fldr = Nothing
End Function
Explanation
strPath = Thisworkbook.Path
Is assigned to the String variable StrPath
the Excel file directory.
If strPath <> "" Then
If you find the file path, that is, the strPath variable is not empty, export all graphics as PNG.
Else
Otherwise:
MsgBox "A pasta do arquivo não foi encontrada - Escolha a pasta."
Shows the message that the file was not found.
strPath = GetFolder
Calls the Getfolder function to choose a folder.
GoTo Inicio
Back to Inicio:
The spreadsheet has only a fixed graph?
– danieltakeshi
Good morning Danieltajeshi, the spreadsheet has other charts, I need to save these charts and put them in a report. The system has ADO connection to Acsses database.
– Guinho
I wrote a reply in VBA, because the tag was only with Excel. But you are using VBA or VB.NET? If it is VB my answer is wrong. Please edit the Tags of your post correctly.
– danieltakeshi