Saving (exporting) Excel charts and tables as PNG files, ideally with VBA

I have several charts and tables in Excel worksheets that I need to save as PNG files. I have a co-worker who has been doing that but I'm supposed to be smarter than him so I don't want to ask him how he does it if I don't have to! :- )  Ideally they need to be saved as standalone images not as images on a worksheet, but that may not be essential.

Thanks,
John
LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following code to do what you are looking for..

Sub savechr()
    Dim cht As ChartObject
    Dim xpath As String

    Dim ch As Chart



    xpath = "Your Path here\"

    For Each cht In ActiveSheet.ChartObjects
        cht.Select
        ActiveChart.Export Filename:=xpath & cht.Name, Filtername:="PNG"
    Next cht

    Range("a1").Select



End Sub

Open in new window


Saurabh..
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Thanks, Saurabh, this is great! The only problem is that I need to save the charts not as cht.Name but as the title of the chart. I tried cht.Title but it bugged of course.

Thanks,
John
Saurabh Singh TeotiaCommented:
Use this...

Sub savechr()
    Dim cht As ChartObject
    Dim xpath As String

    Dim ch As Chart
    xpath = "Your Path here\"

    For Each cht In ActiveSheet.ChartObjects
        cht.Select
        ActiveChart.Export Filename:=xpath & ActiveChart.ChartTitle.Text, Filtername:="PNG"
    Next cht

    Range("a1").Select
End Sub

Open in new window


Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

John CarneyReliability Business Tools Analyst IIAuthor Commented:
Thanks, Saurabh! You made my day :- )   John
Saurabh Singh TeotiaCommented:
John.. You are welcome.. Always happy to help.. :-)
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Hi Saurabh, something new came up and I need to modify an existing macro that copies a chart in Excel and pastes it into a PPT slide as a chart. If it's not asking too much, is it possible to paste the chart into the slide as a PNG file, perhaps with a slight modification of line 7?
Sub ChartToPresentation()
Sheets(i).ChartObjects("Chart 1").Copy
    Set PPApp = GetObject(, "Powerpoint.Application")
    Set PPPres = PPApp.ActivePresentation
    PPApp.ActiveWindow.ViewType = ppViewSlide
    PPPres.Slides(i - 2).Select
    With PPPres.Slides(i - 2).Shapes.Paste
        If i = 6 Then
        .top = 60
        ElseIf i = 7 Or i = 9 Then
        .top = 120
        Else
        .top = 80
        End If
        .Left = 46
        .LinkFormat.BreakLink
    End With
ThisWorkbook.Activate
End Sub

Open in new window

Thanks!
John
Saurabh Singh TeotiaCommented:
This will be...

PPPres.Slides(i - 2).Shapes.PasteSpecial (ppPasteJPG)

Open in new window


Saurabh...
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Thanks, Saurabh!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.