Export Chart as Image

– Selva V Pasupathy, HSBC Global Resourcing, Hyderabad


The following code will help you export a selected chart as an image and will be saved in the same folder as the workbook.

Copy the following code and paste it in a code module

'__________________________________________________
'**************************************************
'*
'* MODULE NAME:     <<>>
'* AUTHOR:          Selva V Pasupathy,
'*                  HSBC Global Resourcing, Hyderabad
'* CONTACT:         socko@rediffmail.com
'__________________________________________________
Option Explicit
Sub Export_Chart2GIF()
'   Before exporting a chart need to select one
  Const PathSEP = "/"
  Const sPicType$ = ".gif"
  Dim sChartname
  Dim myPath
  Dim thisbook
  Dim myChart As ChartObject
    
    
  On Error Resume Next
'   If there is no chart object on this sheet,
'   let the user know about this
  Set myChart = ActiveSheet.ChartObjects(1)
  If myChart Is Nothing Then
    MsgBox "There are no charts on this sheet", 0
    Exit Sub
  End If

  If ActiveChart Is Nothing Then
    MsgBox "Please select a chart before exporting ", 0
    Exit Sub
  End If
    
    
ProcStart:
sChartname = Application.InputBox("Please Specify a name " & _
              "for the exported chart" & vbCr & _
              "There is no default name available" & vbCr & _
              "The chart will be saved in the same " & _
              "folder as this file", "Chart Export", "")

'   User presses "OK" without entering a name
  If sChartname = Empty Then
    MsgBox "You have not entered a " & _
        "name for this chart", _
        , "Invalid Entry"
    GoTo ProcStart
  End If
    
'   Test for Cancel button
    If sChartname = "False" Then
        Exit Sub
    End If

'   If a name was given, chart is exported
'   as a picture in the same
'   folder location as their current file
  thisbook = ActiveWorkbook.Path
  myPath = thisbook & PathSEP & _
        sChartname & sPicType
  ActiveChart.Export Filename:= _
      myPath, FilterName:="GIF"
End Sub


Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: