The XL2GIF macro on this page was graciously supplied by Harald Staff
The subroutine to invoke is GIF_Snapshot to convert a range of cells to a .GIF file.Option Explicit 'Harold Staff -- see http://www.mvps.org/dmcritchie/excel/xl2gif.htm 'XL2GIF_module -- GIF_Snapshot Dim container As Chart Dim containerbok As Workbook Dim Obnavn As String Dim Sourcebok As Workbook Function SelectArea() As String Dim Internrange As Range On Error GoTo Brutt Set Internrange = Application.InputBox("Select " _ & "range to be photographed:", "Picture Selection", _ Selection.AddressLocal, Type:=8) SelectArea = Internrange.Address Exit Function Brutt: SelectArea = "A1" End Function Function sShortname(ByVal Orrginal As String) As String Dim iii As Integer sShortname = "" For iii = 1 To Len(Orrginal) If Mid(Orrginal, iii, 1) <> " " Then _ sShortname = sShortname & Mid(Orrginal, iii, 1) Next End Function Private Sub ImageContainer_init() Workbooks.Add (1) ActiveSheet.Name = "GIFcontainer" Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Worksheets(1).Range("A1") ActiveChart.Location Where:=xlLocationAsObject, _ Name:="GIFcontainer" ActiveChart.ChartArea.ClearContents Set containerbok = ActiveWorkbook Set container = ActiveChart End Sub Sub MakeAndSizeChart(ih As Integer, iv As Integer) Dim Hincrease As Single Dim Vincrease As Single Obnavn = Mid(ActiveChart.Name, Len(ActiveSheet.Name) + 1) Hincrease = ih / ActiveChart.ChartArea.Height ActiveSheet.Shapes(Obnavn).ScaleHeight Hincrease, _ msoFalse, msoScaleFromTopLeft Vincrease = iv / ActiveChart.ChartArea.Width ActiveSheet.Shapes(Obnavn).ScaleWidth Vincrease, _ msoFalse, msoScaleFromTopLeft End Sub Public Sub GIF_Snapshot() Dim varReturn As Variant Dim MyAddress As String Dim SaveName As Variant Dim MySuggest As String Dim Hi As Integer Dim Wi As Integer Dim Suffiks As Long Set Sourcebok = ActiveWorkbook MySuggest = sShortname(ActiveSheet.Name) ImageContainer_init Sourcebok.Activate MyAddress = SelectArea If MyAddress <> "A1" Then SaveName = Application.GetSaveAsFilename( _ initialfilename: =MySuggest _ & ".gif", fileFilter:="Gif Files (*.gif), *.gif") Range(MyAddress).Select Selection.CopyPicture Appearance:=xlScreen, _ Format:=xlBitmap If SaveName = False Then GoTo Avbryt End If If InStr(SaveName, ".") Then SaveName _ = Left(SaveName, InStr(SaveName, ".") - 1) Selection.CopyPicture Appearance:=xlScreen, _ Format:=xlBitmap Hi = Selection.Height + 4 'adjustment for gridlines Wi = Selection.Width + 6 'adjustment for gridlines containerbok.Activate ActiveSheet.ChartObjects(1).Activate MakeAndSizeChart ih:=Hi, iv:=Wi ActiveChart.Paste ActiveChart.Export Filename:=LCase(SaveName) & _ ".gif", FilterName:="GIF" ActiveChart.Pictures(1).Delete Sourcebok.Activate End If Avbryt: On Error Resume Next Application.StatusBar = False containerbok.Saved = True containerbok.Close End Sub
Harold Staff's code appears above, my implementation notes and comments follow.
![]()
<img src="xl2gif.gif">
![]()
<img src="xl2gif3c.gif" border="0">
![]()
<img src="xl2gif3c.gif" border="3">
The example on the left is a GIF file created by XL2GIF by Harald Staff and is described on this page. The file is 1471 bytes (197 x 53 pixels). Related articles can be found on my Home page in the HTML area, and the can be found here. (The macro is xl2GIF) The first example does not include adding some pixels in the VBA code to account for cell boundaries. Left this one in to show the difference, since you are working with both the VBA and the HTML. The missing or faint portion is caused by the VBA code not providing sufficient dimensions, not to be confused with Border="0" in HTML coding for image (IMG).
The example in color is 201 x 63 pixels, Harold's code was improved a little to provide for the extra pixels used by cell boundaries in the VBA code.
The last example includes a cell boundary in the HTML image source.
Some Additional Code Examples
You can change the filter to whatever you want. I notice that if you convert a Word document to HTML you will get both .gif and .pngSub expchart() ' Efstratios Malasiotis,("Stratos"), 2000-06-19 programming ActiveChart.Export FileName:="D:\MyChart.gif", FilterName:="GIF" ActiveChart.Export FileName:="D:\MyChart.jpg", FilterName:="JPEG" ActiveChart.Export FileName:="D:\MyChart.png", FilterName:="PNG" 'To export a picture, first paste it in an empty chartobject ' and then export the chart. End Sub
384 bytes
  A B C 1 A1 B1 C1 2 A2 B2 C2 3 A3 B3 C3
473 bytes
  A B C 1 A1 B1 C1 2 A2 B2 C2 3 A3 B3 C3 The example on the left was created by an earlier version of XL2HTMLx and is 384 bytes, and includes the column and row headers, the example below it is a more current vesion that includes color (473 bytes). The XL2HTML macro will produce the table without the Excel row and column headings. Related articles can be found on my Home page in the HTML area, Excel to HTML -- information & downloads and the coding can be found here. (The macro is xl2HTML)
Adjustment spacing can be forced by including char(160) or (non-breaking space) characters, or by fiddling with cell boundaries and spacing in the generated code. The second appears more compact because the macro now generates: cellspacing="0" cellpadding="0"
For what it is worth, if you were to select the area A1:C3, copy it, hold shift and create a picture, then paste that picture into an HTML Word Document your document would be 5,737 bytes. So you can instantly realize the overhead involved with conversions with so-called web features. frankly I do not have 5K bytes to spare, but it greatly increases download time and storage space. Also while it may reproduce the original document more correctly it usually produces a very bad HTML document overridding features that HTML does so much better. The complete document you are reading here is 7KB + 2 KB for the GIF file. for the image included.
Paste Picture You can copy a chart, then use Edit, copy picture and then simply paste into another Office application (ctrl+v). Office application.
Create a
PictureYou can create a picture from a group of selected cells as follows:
select contiguous cells, copy (ctrl+c), use Shift+Edit. You can copy picture, then using normal paste (ctrl+v), or even create a GIF file as described below. These two taken together can substitute for Harold's code.
GIF files from
figures Public Sub ChartToGIF() 'David McRitchie 2000-04-17 Dim TPath As String, SName As String, Istr As String TPath = "c:\temp\" SName = "chart01" ActiveChart.Export filename:=TPath & SName & ".gif", _ filtername:="GIF" Istr = "<img href=""" & TPath & SName & ".gif"" Alt=" & SName & """>" MsgBox ActiveChart.Name & " image can be included in HTML by pasting this code" & _ Chr(10) & Istr End SubYou can choose your filter in the above as GIF, JPEG, TIF, or PNG.
To store in same path as Excel file use ThisWorkbook.Path
If you prefer you can use an inputbox. using a default to supply filename.More Related Postings
The following list was started with the following Google Usenet Achive Search:
gif & save & chart* & sub & ~g *excel*Harold Staff has many great postings, thought I'd list some here that almost constitute web pages.
- Saving multiple charts as multiple gifs, Tim Williams 1999-09-10.
Save as .GIF files all charts found in all of the worksheets of the active workbook.Pasting Pictures
- Addin to copy selected sheets, 2001-03-16, Addin to select which sheets from the active workbook should be copied into a fresh new workbook, and in there which sheets should have their formulas removed and replaced with the cell values. This is a text posting and tells how to create the addin with the buttons and everything, from beginning to end, and as Harald said -- It's a very useful -- and I believe him.
- pasting a picture from a URL to Excel at a specific location. Norman Harker, 2001-10-11, programming.
ActiveSheet.Pictures.Insert("D:\mypictures\sample.jpg").Select
An additional example from Tom Ogilvy 2001-11-14 to obtain picture from local files and place it at and set it to the dimensions of the selected cell.Related Web Pages
- Excel to HTML on my site. xl2html and xl2htmlx macros.
- Some Examples on my Toolbars page.
(you can copy these 16 bit x 16 bit buttons)
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
- Paste Picture Creates a bitmap file from a chart, a range, or from the clipboard as a bitmap or metafile. Choices are an image as either a bitmap (better for same-size copies) or a metafile (better when zooming/stretching).
- PastePicture The entry point for the routine
- CreatePicture Private function to convert a bitmap or metafile handle to an OLE reference
- fnOLEError Get the error text for an OLE error code- Export GiF and add-in at Rob's Excel VBA Pages. Rob Bruce has VBA utilities on his site for Excel, his main newsgroup is probably Excel-L. [LOST IN SPACE: Terminate the new browser window to return here.]
- Harald Staff has also written a document on Pivot Tables, you can compare his to Microsoft's. If you have Excel 2000 you can paste Harald's example into your spreadsheet and work along step by step with the descriptions.
- Webbify Excel, Dermot Balson -- Update websites from Excel, Download files without web queries, An Excel web browser, An excel web server.
You are one of many distinguished visitors who have visited my site here or in a previous location since this page was created on March 4, 2000. Return to TOP.The primary feature on this page is the XL2GIF macro provided by provided by Harold Staff.
Visit [my Excel home page] [Index page] [Excel Onsite Search] [top of this page]
Please send your comments concerning this web page to: David McRitchie mailto:DMcRitchie@msn.com.