cellmatrix.net

Size and Export Embedded Charts as .GIF Images

To add images of embedded charts to this weblog, I use the VBA procedure below. To run the procedure, first make sure the specifications are correct. You can make changes to the chart height, width, file name, and file path. When all of the inputs are correct, click on (activate) the embedded chart and run the macro.

Sub ExportChart()

    
Dim Cht As ChartObject
    Dim Path
As String
    Dim FileName
As String
    
    Set Cht
= ActiveChart.Parent

    Cht
.Height = 210
    Cht
.Width = 336
    FileName
= "2008032301.gif"
    
Path = "C:\Program Files\"
    
    
ActiveChart.Export Path & FileName

End Sub

Please note that the height and width are not really needed. I've added this code only because I like to make sure all of the charts are exactly the same size.


I’ve written a more detailed version that exports to the parent workbook’s directory, and allows the user to enter a file name, from which the routine determines the image file type. It also checks for an existing file with that name.

Sub ExportChart()
  
Dim sChartName As String
  Dim sPrompt
As String
  Dim sDefault
As String
  
  
If ActiveSheet Is Nothing Then GoTo ExitSub
  
If ActiveChart Is Nothing Then GoTo ExitSub
  
  sPrompt
= "Chart will be exported into directory of active workbook." & vbNewLine & vbNewLine
  sPrompt
= sPrompt & "Enter a file name for the chart, including an image file extension (e.g., .png, .gif)."
  
sDefault = ""
  
  
Do
    
sChartName = Application.InputBox(sPrompt, "Export Chart", sDefault, , , , , 2)
    
    If
Len(sChartName) = 0 Then GoTo ExitSub
    
If sChartName = "False" Then GoTo ExitSub
    
    Select
Case True
      
Case UCase$(Right(sChartName, 4)) = ".PNG"
      
Case UCase$(Right(sChartName, 4)) = ".GIF"
      
Case UCase$(Right(sChartName, 4)) = ".JPG"
      
Case UCase$(Right(sChartName, 4)) = ".JPE"
      
Case UCase$(Right(sChartName, 5)) = ".JPEG"
      
Case Else
        
sChartName = sChartName & ".png"
    
End Select
    
    
If Not FileExists(ActiveWorkbook.Path & "\" & sChartName) Then Exit Do
    
    
sPrompt = "A file named '" & sChartName & "' already exists." & vbNewLine & vbNewLine
    sPrompt
= sPrompt & "Select a unique file name, including an image file extension (e.g., .png, .gif)."
    
sDefault = sChartName
    
  Loop
  
  ActiveChart
.Export ActiveWorkbook.Path & "\" & sChartName
ExitSub
:
  
End Sub

A more advanced version is under development for a chart utility; it features a file-save dialog that lets the user browse to any directory, and also allows the user to overwrite an existing file.

Posted by Jon Peltier  on  03/24  at  05:06 AM

Comment Form
Name: (Required)
E-Mail Address: (Optional)
Location: (Optional)
Web Site Address: (Optional)

pMcode is allowed for comment formatting // pMcode Quick Reference

Remember my personal information?
Notify me of follow-up comments?

Before submitting your comment, please enter the phrase you see below:


About

Formulas, Charts, and Models Created with Microsoft Excel.

Read more...

Statistics

  • Total Entries - 83
  • Current Viewers - 3
  • Days Online - 467

Categories

Entries by Day

July 2008
S M T W T F S
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2

Excel Web Logs

Syndicate

Validate

My Resources...

Copyright © 2007 - 2008