Export Excel Range to a Picture File

November 13, 2008JP20 CommentsRate This ArticlenewLinks to this article


(See my followup post Export Excel Range to a Picture File, Redux for updated VBA.)

    If you ever needed to capture a range, for pasting into a document or email, you'll need to save it as an image first. If so, you might find this routine useful. It takes a contiguous range, starting in A1, and creates a GIF file with a snapshot of the range.

    A temporary, intermediate chart is used, so we can use its Export Method to create the GIF. The chart is then deleted. Through trial and error, I guessed at the appropriate chart size. I'm using Windows XP with a resolution of 1024×768, so you might need to adjust the constants' values as appropriate.

Sub CopyRangeToGIF()
' save a range from Excel as a picture
Dim rng As Excel.Range
Dim cht As Excel.ChartObject

Const strPath As String = "C:\"

Application.ScreenUpdating = False

Set rng = Range("A1").CurrentRegion

rng.CopyPicture xlScreen, xlPicture

Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width + 10, rng.Height + 10)

cht.Chart.Paste

cht.Chart.Export strPath & "myfile.gif"

cht.Delete

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

    This code will only create the picture. You'll probably want to do something with it.

    If you want to add it to a Word document, try this:

Dim wrdApp As Object ' Word.Application
Dim wrdDoc As Object ' Word.Document
Set wrdApp = CreateObject("Word.Application")
'wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
wrdDoc.InlineShapes.AddPicture "C:\myfile.gif"

    To send it as an attachment in an Outlook email, try this:

Dim olApp As Object ' Outlook.Application
Dim Msg As Object ' Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set Msg = olApp.CreateItem(0)

With Msg
.To = "coworker@mycompany.com"
.Body = "Check out this range!"
.Attachments.Add "c:\myfile.gif"
.Send
End With

    If you want to send the range in the body of an email, you're better off using the technique described here.

    And last but not least, if you wanted the picture in a PowerPoint slide, this code will create a new presentation and insert the picture into a slide. Unlike the code snippets above for Outlook and Word, which use the saved GIF copy, for PowerPoint you'll need to call this code right after executing the Range.CopyPicture Method.

Dim pptApp As Object ' PowerPoint.Application
Dim pptPres As Object ' PowerPoint.Presentation
Dim pptSlide As Object ' PowerPoint.Slide

Set pptApp = CreateObject("PowerPoint.Application")
Set pptPres = pptApp.Presentations.Add
Set pptSlide = pptPres.Slides.Add

pptSlide.Shapes.Paste

[Update 11/14/2008]: CopyRangeToGIF sub was updated based on suggestion from Jon Peltier. No need for complicated calculations!

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

19 Response(s) to Export Excel Range to a Picture File ↓

  1. Jon Peltier says:

    I have a suggestion. This segment (in ChartObjects.Add):

    ColumnsRatio * lColsCount, RowsRatio * lRowsCount

    assumes equal row heights and column widths. Instead use:

    rng.Width, rng.Height

    Then add a small amount, a few points, because the chart area needs a small margin to fit within the chart object.

  2. JP says:

    Works. I assumed that the range was calculated in pixels, while the ChartObject width/height was calculated in points, so I started fiddling with an algorithm to calculate the correct size.

  3. Partha says:

    I was looking for this for a long time. Thank you very much for this. you are a star…..

  4. Guppy says:

    I the first part of code you have this line:
    cht.Chart.Export strPath & "myfile.gif"

    My Excel 2007 will not accept & and I have not found other solutions.

  5. Guppy says:

    Thanks, I'll test the solution on the link you give.

    Meanwhile I did a workaround by setting the path including a file name:
    Const strPath As String = "C:\users\frank\desktop\chart.bmp"
    I want to make gif and not bmp, but choosing gif they look horrible/distorted.

  6. JP says:

    I'm confused about what the problem is. Is it just the path syntax? I assumed the problem was the method call.

  7. Guppy says:

    As I include the file name in the path string I only need
    cht.Chart.Export strPath
    so I avoid the '&' causing trouble in:
    cht.Chart.Export strPath & "myfile.gif"

  8. JP says:

    I see the problem now. WordPress had converted the "&" to its HTML equivalent, hence the error. If you just change the "&" to an ampersand "&", the original code should work as written. But your solution works as well.

  9. jack says:

    It works, thanks

    jack

  10. Kolorowanki says:

    Great tutorial, I will try your way. I only drawing so I will pass your post address to my programmer.
    Thanks for sharing.
    Regards,
    Matt Kolorowanki,
    Illustrator

  11. Sapan says:

    Is there a way to avoid the chart boundary coming up in the image. I just want to see the image of the selected Range. When we create a chart object, it adds its own boundary to the image. Is there a way to avoid it?

  12. Jon Peltier says:

    Sapan -

    Format the chart so that it has no border, and either a white background (the default) or a transparent background,

  13. Sapan says:

    Is there a macro by which i can change the chart settings?

  14. Jon Peltier says:

    Record a macro while changing the settings, then use this syntax to modify your code.

  15. AlexJ says:

    Sending the picture as an attached file is fine, but I would like to copy the image in Excel and paste it (as a viewable picture) into the body of the outlook message.

    What code can be used instead of ".Attachments.Add "c:\myfile.gif" ?

    (Good at Excel VBA, suck at Outlook)

  16. AlexJ says:

    oops – found it. Ron deBruin has a good writeup at http://msdn.microsoft.com/en-us/library/ff519602(office.11).aspx

  17. Steve says:

    How do you position the picture on the PowerPoint slide once you have pasted it?


1 Trackback(s)

Check out what others are saying about this post...

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated August 24, 2010 @ 5:56 pm