VBA: How to Export a Picture object as PNG without Copying into a Chart?

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
I need to export a range to a PNG file.

Currently what I do is to
1. copy-paste that range into a picture object
2. copy-paste that picture to a chart to export

The problem is that the 2nd step does not preserve the quality (the clearness) of the picture, even when they are the same size. The exported png is much more blurred than manually copying that picture object to MS Paint then saving, where the quality is preserved (the texts in that png are sharp just like seen in Excel cells).

Is there a way to avoid using Chart as a step? Or, is there a way to export a picture while keeping its quality?


Below is the code that I currently have (revised from codes found on the internet), which outputs blurred text images.

I have Excel 2010 64 on Win7 64.


Code:
Sub RangetoPNG(thatRange as Range)        
    Dim MyChart As String, MyPicture As String
    Dim PicTop As Long, PicLeft As Long, PicWidth As Long, PicHeight As Long
    
    '----- Step 1 --------
    thatRange.Copy
    Range("A1").Select
    With ActiveSheet.Pictures.Paste
        .Select
        .Copy        
    End With


    MyPicture = Selection.Name
    With Selection
           PicTop = .ShapeRange.Top
           PicLeft = .ShapeRange.Left
           PicHeight = .ShapeRange.Height
           PicWidth = .ShapeRange.Width
    End With


    Selection.Delete


    '----- Step 2 --------
    With ActiveSheet.ChartObjects.Add(Left:=PicLeft, Top:=PicTop, Width:=PicWidth, Height:=PicHeight)
        .Name = "ChartName1"
        .Activate
    End With
    
    ActiveChart.Paste
    ActiveSheet.ChartObjects("ChartName1").Chart.Export "c:\output.png"
    ActiveSheet.ChartObjects("ChartName1").Delete
    
End Sub

Thank you.
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Did you ever figure out this problem? I think I am having the same issue. I can get images into excel but I wanted them as individual PNG files with the name of a relative cell.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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