Export an Image from Excel Sheet to Hard Drive-VBForums
Results 1 to 5 of 5

Thread: Export an Image from Excel Sheet to Hard Drive

  1. #1

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Export an Image from Excel Sheet to Hard Drive

    I was thinking to include this in the code bank for a long time but didn't think it was that important but after when I replied to one of the threads today, I did a quick search in the forum and found that many members have asked this question in the past (including me) So here it is. I am pasting the code for your reference. Hope it benefits someone... If it does then do leave a comment here

    Excel unfortunately doesn't allow you to directly export jpeg, gif images. However It does let you easily export flowcharts, charts etc... so the key is to trick Excel. And we can do that by creating a temp chart and pasting the picture in the chart. We need to work with the dimensions a little bit but after that it very easy to export the image. Here is the code...

    I have commented the code so that it would be easy to understand...

    vb Code:
    1. Option Explicit
    2.  
    3. 'You need to select a picture before running this code
    4. 'else it will give you error'
    5. Sub PictureExport()
    6.     Dim TempChart As String, Picture2Export As String
    7.     Dim PicWidth As Long, PicHeight As Long
    8.      
    9.     Picture2Export = Selection.Name
    10.      
    11.     'Store the picture's height and width  in a variable
    12.     With Selection
    13.         PicHeight = .ShapeRange.Height
    14.         PicWidth = .ShapeRange.Width
    15.     End With
    16.      
    17.     'Add a temporary chart in sheet1
    18.     Charts.Add
    19.     ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    20.     Selection.Border.LineStyle = 0
    21.     TempChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
    22.      
    23.     With ActiveSheet
    24.         'Change the dimensions of the chart to suit your need
    25.         With .Shapes(TempChart)
    26.             .Width = PicWidth
    27.             .Height = PicHeight
    28.         End With
    29.            
    30.         'Copy the picture
    31.         .Shapes(Picture2Export).Copy
    32.            
    33.         'Paste the picture in the chart
    34.         With ActiveChart
    35.             .ChartArea.Select
    36.             .Paste
    37.         End With
    38.            
    39.         'Finally export the chart
    40.         .ChartObjects(1).Chart.Export Filename:="Sample.jpg", FilterName:="jpg"
    41.         'Destroy the chart. You may want to delete it...
    42.         .Shapes(TempChart).Cut
    43.       End With
    44. End Sub
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  2. #2

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Export an Image from Excel Sheet to Hard Drive

    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    New Member
    Join Date
    Dec 2011
    Posts
    1

    Re: Export an Image from Excel Sheet to Hard Drive

    OR you could just save your excel sheet as a web page. There will be a file and a folder. All your pictures will be in that folder in their original file type.

  4. #4
    New Member
    Join Date
    Dec 2011
    Posts
    1

    Re: Export an Image from Excel Sheet to Hard Drive

    Quote Originally Posted by kyle012012 View Post
    OR you could just save your excel sheet as a web page. There will be a file and a folder. All your pictures will be in that folder in their original file type.
    I agree with your thoughts! Thanks!


    ________________________________
    r4 3dsps3 jb2crown3ds
    Last edited by mandykk; Dec 30th, 2011 at 08:40 PM.

  5. #5
    Lively Member
    Join Date
    Aug 2011
    Posts
    103

    Re: Export an Image from Excel Sheet to Hard Drive

    [QUOTE=mandykk;4110797]I agree with your thoughts! Thanks!

    using the VBA code from the first post, the acquired image has a lower quality (is blurry somehow) than when you save the excel file as web page and use the image in the HTML folder.
    You see in this world there's two kinds of people, my friend. Those with loaded guns, and those who dig. You dig.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.