Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-09-2012, 08:40 PM
omahadivision omahadivision is offline Making VBA select chart and then export as picture Windows 7 32bit Making VBA select chart and then export as picture Office 2007
Novice
Making VBA select chart and then export as picture
 
Join Date: Oct 2012
Posts: 28
omahadivision is on a distinguished road
Default Making VBA select chart and then export as picture

Hello All,

So I have what seems to be an insanely easy coding question, but it's driving me crazy. So I have this code that selects a chart and then copies it, and I wanted to make it save the chart as a picture instead of copying it. Here is the copying code:

Code:
    Dim objChart As ChartObject
       
    On Error Resume Next
    Set objChart = ActiveSheet.ChartObjects(1).Select
    
    Selection.CopyPicture _
    Appearance:=xlScreen, Format:=xlBitmap
That code works fine, so I thought I'd do

Code:
       Dim objChart As ChartObject
       
    On Error Resume Next
    Set objChart = ActiveSheet.ChartObjects(1).Select
    
    Selection.Export Filename:="C:\test.jpg", FilterName:="jpg"
Nothing ever shows up in C:\ or any other folder I've tried. I also wondered if I needed to set objChart as something else so that it as already selected, based on something I saw online:

Code:
 Dim objChart As ChartObject
        Dim slcChart As ChartObject
       
   On Error Resume Next
    Set objChart = ActiveSheet.ChartObjects(1).Select
    Set slcChart = ActiveSheet.ChartObjects(1)
    
    If ActiveChart Is Nothing Then
       MsgBox "No charts have been detected on this sheet", 0
        End If
    

    slcChart.Export Filename:="C:\test.jpg", FilterName:="jpg"
Still doesn't work. No error messages but no file either. I only have one chart in my Excel file. Can you see anything I might have done wrong?

-Dave
Reply With Quote
  #2  
Old 10-12-2012, 12:07 PM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Making VBA select chart and then export as picture Windows 7 32bit Making VBA select chart and then export as picture Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

So it's the chart class, not the chartobject class which has an export method. This worked fine for me (I set up a chart before running the code):

Code:
Sub foo()
    Dim cto As ChartObject
    Dim cht As Chart
    
    'get a reference to the chartobject on the sheet
    Set cto = Sheet1.ChartObjects(1)
    
    'get a reference to the chart
    Set cht = cto.Chart
    
    'export the chart as a jpg
    cht.Export "C:\Temp\MyChartPic.jpg", "JPG"
    
End Sub
__________________
Colin

RAD Excel Blog
Reply With Quote
  #3  
Old 10-28-2012, 07:53 PM
omahadivision omahadivision is offline Making VBA select chart and then export as picture Windows 7 32bit Making VBA select chart and then export as picture Office 2007
Novice
Making VBA select chart and then export as picture
 
Join Date: Oct 2012
Posts: 28
omahadivision is on a distinguished road
Default

Thanks! It works now! Sorry about the delay; I honestly lost the link

-Dave
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code to group picture to export as one RenaatS PowerPoint 3 03-05-2012 12:40 PM
Export resource custom field to excel chart jtorres Project 0 03-15-2011 05:16 AM
Can't select or edit chart data itguy PowerPoint 2 09-19-2010 10:13 AM
Making a bar chart with several data entries Blondie1966 PowerPoint 2 04-28-2010 03:00 PM
How To Export A Picture From Word? abrogard Drawing and Graphics 0 08-10-2009 06:27 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:42 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft