Converting and Excel file to a .jpg file....

giraffe

New Member
Joined
Mar 11, 2002
Messages
5
I need help converting an excel spreadsheet into a .jpg file so that it can be viewed using Adobe Acrobat.

Can anyone help????
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Approach one.

The following code can, rather "painlessly", export an image to a BMP file.

It uses the PastePicture code from Stephen Bullen's site:

http://www.bmsltd.co.uk/Excel/Default.htm


<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> SaveRngAsBMP(Rng <SPAN style="color:#00007F">As</SPAN> Range, FileName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
    Rng.CopyPicture xlScreen, xlBitmap
    SavePicture PastePicture(xlBitmap), FileName
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestIt()
    <SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, Fn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Rng = Range("A1:A5")
    Fn = "C:\MyFile.bmp"
    SaveRngAsBMP Rng, Fn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Approach two.

The Chart object has a .Export method that enables to save directly to a JPG file... however, this method isn't very "pretty" because it must use that object as an intermediate, to get the final result. Don't need the PastePicture code from Stephen Bullen.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> SaveRngAsJPG(Rng <SPAN style="color:#00007F">As</SPAN> Range, FileName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
    <SPAN style="color:#00007F">Dim</SPAN> Cht <SPAN style="color:#00007F">As</SPAN> Chart, bScreen <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, Shp <SPAN style="color:#00007F">As</SPAN> Shape
    bScreen = Application.ScreenUpdating
    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Cht = Workbooks.Add(xlChart).Charts(1)
    Cht.ChartArea.Clear
    Rng.CopyPicture xlScreen, xlPicture
    Cht.Paste
    <SPAN style="color:#00007F">With</SPAN> Cht.Shapes(1)
        .Left = 0
        .Top = 0
        .Width = Cht.ChartArea.Width
        .Height = Cht.ChartArea.Height
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    Cht.Export FileName, "JPEG", <SPAN style="color:#00007F">False</SPAN>
    Cht.Parent.Close <SPAN style="color:#00007F">False</SPAN>
    Application.ScreenUpdating = bScreen
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestIt2()
    <SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, Fn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Rng = Range("A1:H21")
    Fn = "C:\MyFile.jpg"
    SaveRngAsJPG Rng, Fn
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
This looks exactly like what I need, but I'm a noob when it comes to programming in Excel. How do I add the code above?

Thanks!

-Z
 
Upvote 0
Ok.. I opened the Visual Basic editor (Alt F11) and added a new module. I clicked the run button and I had the .JPG file in c:\.

Is there an easier way to run this than opening the VB editor each time and clicking play?

Thanks!

-Z
 
Upvote 0
Hi all,

I know this is a really old post but it helped me immensely so I though I'd post my final code without the tags that are on Juan's nifty post in thread 2 for anyone else who may need to save a range as a *.jpg or *.bmp image. I have made a few changes to for my own needs and to get it running seamlessly (needed to activate (select) the chart).

Regards,

Robert

Code:
Option Explicit
'https://www.mrexcel.com/board/threads/converting-and-excel-file-to-a-jpg-file.54296/

'The Chart object has a .Export method that enables to save directly to a JPG file...
'however, this method isn't very "pretty" because it must use that object as an intermediate, to get the final result.
'Don't need the PastePicture code from Stephen Bullen.

Private Sub SaveRngAsImage(Rng As Range, FileName As String, FileType As String)

    Dim Cht As Chart, Shp As Shape

    Set Cht = Workbooks.Add(xlChart).Charts(1)
    Cht.Activate 'Clunky but necessary
    Cht.ChartArea.Clear
    Rng.CopyPicture xlScreen, xlPicture
    Cht.Paste
    With Cht.Shapes(1)
        .Left = 0
        .Top = 0
        .Width = Cht.ChartArea.Width
        .Height = Cht.ChartArea.Height
    End With
    Cht.Export FileName, StrConv(FileType, vbUpperCase), False
    Cht.Parent.Close False

End Sub
Sub CreateRngAsImage()

    Dim Rng As Range, Fn As String
    Dim strFileType As String
   
    Application.ScreenUpdating = False
    Set Rng = Range("A1:H21") 'Change to suit.
    Fn = "C:\TestJpeg.bmp" 'Full path for the image to be saved. Change to suit.  Extension could be either "bmp" or "jpg"
    strFileType = Right(Fn, Len(Fn) - InStrRev(Fn, "."))
    Call SaveRngAsImage(Rng, Fn, strFileType)

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
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