Exporting Graphics from Excel

Exporting Graphics from Excel

Here you find how to export any embedded graphs and images from excel. There is no direct export method for the embedded images. But here is the workaround for the same,

Sub Exportimage()

'declare the variables

     Dim pic As Object
     Dim MyChart As String, MyPicture As String
     Dim PicWidth As Long, PicHeight As Long
     Dim ss, ds As Worksheet
     Application.ScreenUpdating = False
     On Error GoTo Finish
'the image is embeded in sheet named input, we use it as source sheet.     
     Set ss = Worksheets("input")
'the chart will be embededed in the sheet named chart, we use it for destination sheet
     Set ds = Worksheets("Chart")
'we find the picture named "cmo" in the input sheet which is about to be exported.    
     Set pic = ss.Shapes("cmo")
'we read the size of the image to maintain the same size while exporting.     
     With pic
           PicHeight = .Height
           PicWidth = .Width
     End With

     
'add a chart on a sheet named "chart"
    
     Charts.Add
     ActiveChart.Location Where:=xlLocationAsObject, Name:="Chart"
     Selection.Border.LineStyle = 0
     MyChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
    
'we prepare the chart area to hold the image.     
     With ds
           With .Shapes(MyChart)
                 .Width = PicWidth
                 .Height = PicHeight
           End With
           
     End With



'copy and paste the image on the chart area.

     pic.Copy


     With ds

           With ActiveChart
                 .ChartArea.Select
                 .Paste
           End With
 'export the chart as "mypic.jpg" in the folder C:\test.

           .ChartObjects(1).Chart.Export Filename:="C:\test\MyPic.jpg", FilterName:="jpg"
           .Shapes(MyChart).Cut
     End With

'chartobjects have direct export method.  All shapes opbjects  could be exported in this way without any glitch.
    
     Application.ScreenUpdating = True
     Exit Sub
     
Finish:
     MsgBox "You must select a picture"
End Sub


要查看或添加评论,请登录

Lakshminarasimhan S.的更多文章

社区洞察

其他会员也浏览了