Export pivot table to .gif image file - buggy!

wazimu13

New Member
Joined
Apr 29, 2013
Messages
26
Hi

I have the following code which works in some cases but in others it generates an actual chart on the resulting image!
Researching this online has been very frustrating and am hoping you gurus can help :) How should the code be modified?

Basically what im trying to do is save several pivot tables as gif files which i then email. People ive asked say the best way
is to export as an image, and someone kindly offered this code.

Im quite new to VBA but cant see
which line is responsible for this behavior and having said that, why does it work flawlessly and all of a sudden just change.

In the attached images, the andy clarke.gif is the output i desire :) while the other image is how it goes wrong :(

Thanks in advance!

Code:
Sub CopyRangeToGIF()
' save a range from Excel as a picture

Dim rng As Excel.Range
Dim cht As Excel.ChartObject
Dim i As Integer

Const strPath As String = "C:\Users\paul\Documents\pics\"
 
Application.ScreenUpdating = False
 
 For i = 4 To Sheets.Count - 6

 Set rng = Sheets(i).UsedRange
 
rng.CopyPicture xlScreen, xlPicture
 
Set cht = Sheets(i).ChartObjects.Add(0, 0, rng.Width + 10, rng.Height + 10)
 
cht.Chart.Paste
 
cht.Chart.Export strPath & Sheets(i).Name & ".gif"
 
cht.Delete
 
ExitProc:

Next i

Application.ScreenUpdating = True
Set cht = Nothing
Set rng = Nothing
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top