Export range as image

kendo679

New Member
Joined
Aug 26, 2016
Messages
25
I found the code below in an old thread somewhere and it works perfectly for exporting a range as an image, the only problem is file size.

When I manually copy and paste into paint, the file is only 150kb, but the macro results in a file size of over 5000kb.

Does anybody here see a way to get the file size down, or have any other tricks to offer?
As you see at the bottom, I'm saving 16 separate images with one button click.

Code:
Option Explicit

Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long

Private Declare Function CloseClipboard Lib "user32" () As Long

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
(PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, _
IPic As IPicture) As Long

'\\ Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

'\\ Declare a UDT to store the bitmap information
Private Type uPicDesc
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
End Type

Private Const CF_BITMAP = 2
Private Const PICTYPE_BITMAP = 1
'-----------------------------------------------------------------------------------
Private Sub SaveRangePic(SourceRange As Range, FilePathName As String)


    Dim IID_IDispatch As GUID
    Dim uPicinfo As uPicDesc
    Dim IPic As IPicture
    Dim hPtr As Long

    '\\ Copy Range to ClipBoard
    SourceRange.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    OpenClipboard 0
    hPtr = GetClipboardData(CF_BITMAP)
    CloseClipboard

    '\\ Create the interface GUID for the picture
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With
    
    '\\ Fill uPicInfo with necessary parts.
    With uPicinfo
        .Size = Len(uPicinfo) '\\ Length of structure.
        .Type = PICTYPE_BITMAP '\\ Type of Picture
        .hPic = hPtr '\\ Handle to image.
        .hPal = 0 '\\ Handle to palette (if bitmap).
    End With

   '\\ Create the Range Picture Object
   OleCreatePictureIndirect uPicinfo, IID_IDispatch, True, IPic

    '\\ Save Picture Object
    stdole.SavePicture IPic, FilePathName
    
End Sub
'--------------------------------------------------------------------------------
Sub Images()

Dim TARGET As String
TARGET = "C:\Users\KennyS\Desktop\Kenny\5A2 RH5 L2\DISPLAY"


    SaveRangePic Sheet11.Range("A2:AA52"), TARGET & "\AL CHARTS.png"
    SaveRangePic Sheet11.Range("A53:AA103"), TARGET & "\AL CHARTS2.png"
    SaveRangePic Sheet11.Range("A104:AA154"), TARGET & "\AL CHARTS3.png"
    SaveRangePic Sheet11.Range("A155:AA205"), TARGET & "\AL CHARTS4.png"
    
    SaveRangePic Sheet14.Range("A2:AA52"), TARGET & "\AR CHARTS.png"
    SaveRangePic Sheet14.Range("A53:AA103"), TARGET & "\AR CHARTS2.png"
    SaveRangePic Sheet14.Range("A104:AA154"), TARGET & "\AR CHARTS3.png"
    SaveRangePic Sheet14.Range("A155:AA205"), TARGET & "\AR CHARTS4.png"
    
    SaveRangePic Sheet15.Range("A2:AA52"), TARGET & "\BL CHARTS.png"
    SaveRangePic Sheet15.Range("A53:AA103"), TARGET & "\BL CHARTS2.png"
    SaveRangePic Sheet15.Range("A104:AA154"), TARGET & "\BL CHARTS3.png"
    SaveRangePic Sheet15.Range("A155:AA205"), TARGET & "\BL CHARTS4.png"
    
    SaveRangePic Sheet16.Range("A2:AA52"), TARGET & "\BR CHARTS.png"
    SaveRangePic Sheet16.Range("A53:AA103"), TARGET & "\BR CHARTS2.png"
    SaveRangePic Sheet16.Range("A104:AA154"), TARGET & "\BR CHARTS3.png"
    SaveRangePic Sheet16.Range("A155:AA205"), TARGET & "\BR CHARTS4.png"
    
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi,

I use something like this:
Code:
'------------------------------------------------------------------------------
Sub Save_range_as_png()
'------------------------------------------------------------------------------
    'Select range and save as png-file

    Dim Awb As Workbook
    Dim MyChart As Chart
    Dim objPict As Object
    Dim RgCopy As Range
    
    'Remember where we are
    Set Awb = ActiveWorkbook

    'Have user select the range
    On Error Resume Next
    Set RgCopy = Application.InputBox("Select the range to copy / Saveas", _
        "Selection Save", Selection.Address, Type:=8)
    If RgCopy Is Nothing Then Exit Sub
    On Error GoTo 0

    Application.ScreenUpdating = False

    'Copy the range in required picture format
    RgCopy.CopyPicture Appearance:=xlPrinter, Format:=xlPicture
    ActiveSheet.PasteSpecial Format:="Picture" '"Bitmap"
    Set objPict = Selection

    'Switch to this workbook to make sure we have no protection problems
    ThisWorkbook.Activate
    
    With objPict
        .CopyPicture 1, 1 ':=1
        Set MyChart = ActiveSheet.ChartObjects.Add(1, 1, .Width, .Height).Chart
    End With

    With MyChart
        .Paste
        .Export ThisWorkbook.Path & Application.PathSeparator & "Temp.png", "png"
        .Parent.Delete
    End With

    '// cleanup
    objPict.Delete
    Set RgCopy = Nothing
    Set objPict = Nothing
    ThisWorkbook.Saved = True
    Awb.Activate
    Application.ScreenUpdating = True

End Sub

Perhaps you can modify it to your needs?
 
Upvote 0
Thanks DeBeuz,

I tried your code, the only problem is, my range includes charts and they weren't visible in the image once it was saved.
 
Upvote 0
OK, weird... but the next time I ran the macro charts were included.
Now the problem is, the resolution was too poor for my purposes. These images are being sent to a 27" touchscreen display. I know file size vs resolution is always going to be a trade-off, but still, when I paste manually into paint the size is about the same as what your code produced, but the resolution is much better.

Thanks again though for sharing your code. It will be useful to learn from and I appreciate how well you had it commented.
 
Upvote 0
I use the code U posted without problems. I suspect your file size increase has something ro do with the clipboard. I use this code below. HTH. Dave
At the top of the module..
Code:
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long

Then insert the following lines of code in the macro...
Code:
'\\ Save Picture Object
 stdole.SavePicture IPic, FilePathName
Application.CutCopyMode = False
OpenClipboard
EmptyClipboard
CloseClipboard
End Sub
 
Upvote 0
I added the zero behind OpenClipboard, just because I saw it that way above in the module.

Code:
'\\ Save Picture Object
    stdole.SavePicture IPic, FilePathName
    Application.CutCopyMode = False
    OpenClipboard 0
    EmptyClipboard
    CloseClipboard

The code ran as before and the file size is the same, 5217kb for each of the 16 files
 
Upvote 0
Well I really should finish my coffee before posting to the Board. I thought th XL file was increasing. U also already had the open and close API's... whoops. I suggest keeping the EmptyClipboard API and this in your code...
Code:
'\\ Copy Range to ClipBoard
    SourceRange.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    OpenClipboard 0
    hPtr = GetClipboardData(CF_BITMAP)
    EmptyClipboard
    CloseClipboard
Anyways, back to your problem. Trial saving the file(s) as .gif instead of .png Dave
 
Upvote 0
I tried again without that last code you posted,
saved one file as .gif and another as .bmp
both came out with exactly the same file size, 5217kb
 
Upvote 0

Forum statistics

Threads
1,223,284
Messages
6,171,182
Members
452,388
Latest member
Lorenzo_Barry

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