Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting charts as Image sometimes generates empty files

Tags:

excel

vba

I'm doing a macro that exports all the charts in the sheet and then opens Outlook and attaches them. However, I've noticed, several times the charts do export but as 0KB (the file is created, but the image can't be seen) enter image description here

But it doesn't happen to all the charts. Just most of them and sometimes, it generates them all without a problem. (When I execute the code step by step, all charts generate without a problem, also after executing the step by step, then I execute it normally and all charts generate, BUT if I close and reopen the workbook, it gives the same issue, generates only two and the rest are empty files)

Here's the code:

Dim sheetNumber, Size, i As Integer
    Dim chartNames(), FNames() As String
    Dim objChrt As ChartObject
    Dim myChart As Chart


    'Activate Charts Sheet
    Sheets("GRAFICAS").Activate
    'Calculate Number of Charts in Sheet
    Dim chartNumber
    chartNumber = ActiveSheet.ChartObjects.Count
    'Redimension Arrays to fit all Chart Export Names
    ReDim chartNames(chartNumber)
    ReDim FNames(chartNumber)
    'Loops through all the charts in the GRAFICAS sheet
    For i = 1 To chartNumber
        'Select chart with index i
        Set objChrt = ActiveSheet.ChartObjects(i)
        Set myChart = objChrt.Chart
        'Generate a name for the chart
        chartNames(i) = "myChart" & i & ".png"

        On Error Resume Next
        Kill ThisWorkbook.Path & "\" & chartNames(i)
        On Error GoTo 0
        'Export Chart
        myChart.Export FileName:=Environ$("TEMP") & "\" & chartNames(i), Filtername:="PNG"
        'Save path to exported chart
        FNames(i) = Environ$("TEMP") & "\" & chartNames(i)
    Next i

What am I missing?

like image 565
Splendonia Avatar asked Jun 07 '16 14:06

Splendonia


People also ask

How do I export all charts in Excel as images?

A fast way to export all of the charts in an Excel workbook as images is to save a copy of the workbook as a Web Page, as in doing so, Excel will create and export image files for you. Head back to Excel and choose File > Save As.

How do I save a chart as an image file?

Often people want to save their charts as an image file. Excel doesn’t offer this as a native feature in the UI, but you can export a chart using VBA: ActiveChart.Export "C:\My Charts\SpecialChart.png" This command allows you to export your chart in any of the bitmap formats that you have the appropriate export filters for.

Why is my export file empty when I open it?

When it does the .export, the file is empty. Like most problems, it's usually something down to coding / logic that's not right rather than it actually being a bug. I'm just a bit stumped on this one.

How do I export a chart to a bitmap file?

This command allows you to export your chart in any of the bitmap formats that you have the appropriate export filters for. This varies from machine to machine, but three that I’ve always found are .PNG, .GIF, and .JPG. The Chart.Export VBA command exports a chart to a variety of bitmap file formats.


1 Answers

It turns out, it's a random error for Excel 2010-2013 users. However, after some more googling. I encountered the answer here

You just need to add

objChrt.Activate

After selecting the chart. So In my case the final code looks like this:

 For i = 1 To chartNumber
        'Select chart with index i
        Set objChrt = ActiveSheet.ChartObjects(i)
        objChrt.Activate
        Set myChart = objChrt.Chart
        'Generate a name for the chart
        chartNames(i) = "myChart" & i & ".png"

        On Error Resume Next
        Kill ThisWorkbook.Path & "\" & chartNames(i)
        On Error GoTo 0
        'Export Chart
        myChart.Export FileName:=Environ$("TEMP") & "\" & chartNames(i), Filtername:="PNG"
        'Save path to exported chart
        'Application.Wait (Now + #12:00:01 AM#)
        FNames(i) = Environ$("TEMP") & "\" & chartNames(i)
    Next i
like image 108
Splendonia Avatar answered Sep 21 '22 17:09

Splendonia