PowerPoint/VBA
As nice as Excel 2007 is, and much of Office 2007 in general, Microsoft made some annoying decisions with developer access in VBA.
Charts are now Excel objects and their data are based on worksheets. This is pretty nice overall, but Microsoft does not allow access to the sheet data when pasted into Word or PowerPoint. So
Also, changes in the chart are not picked up with the macro reader. So developers can’t use the reader to help figure out the object model and thus write code.
The following routine is one alternative to the macro reader. Select a chart and then run the module. It will stop into break mode and then you can set up watches (right click on them to do so) on each of the three objects. You can select objects (keep the focus on the chart, though) and see the values change.
Public Sub ExploreChartElements()
‘select a chart and then run this, it will break and select various chart elements.. use watches…
Dim oChart As Excel.Chart
Dim oChartGroup As Excel.ChartGroup
Dim oSeries As Excel.seriesSet oChart = ActiveChart
Set oChartGroup = oChart.ChartGroups(1)
Set oSeries = oChart.SeriesCollection(1)Stop
End Sub
Charting is a totally different animal in Office 2007 from its previous 2003- versions. Charts are not MS Graph objects (by default) but Excel.
Working with PowerPoint in VBA is never fun, because managing slides and the objects on the slides is limited, and can not be seen from the interface (like naming a slide or object).
Usually a major new release makes life easier, but in this case, not so. Read the following from PPTFAQ:
If you need to automate charts in PowerPoint 2007, it’d be best if you change your name and move to another town where nobody knows that you do PowerPoint programming.
The chart objects you get in PowerPoint 2007 when you add a new chart expose no methods or properties to VBA. In short, you can’t do anything with them in code.
When you install Office 2007, you have the option of including MSGraph, the application PowerPoint used for charts in previous versions. You’ll want this if you need to work with charts from previous PowerPoint versions and/or to create new charts via automation.
These are not encouraging words. I hoped that SP2 of Office 2007 would solve the answers, but it appears that even though we now have access to the chartdata object in PowerPoint and Word, there is virtually nothing we can do with it.
One would think with a major upgrade like 2007, that automation would get easier. Unfortunately as far as automating graphs in PowerPoint, which is a common need, we are worse off. So there are several choices, none good:
- Configure PP 2007 to default to use MS Graph (see article). This involves using regedit, and obviously is a poor choice if it will be used by other people since they will all have to do the same configurations.
- Use Excel to contain all the graphs and then push them to PP with copy and paste as enhanced metafile. This is probably the best choice, but pasting into a PP file is always difficult since it is so difficult to manage the objects/shapes in the PP slides.
- Use linked graphs in PP to an Excel file as the data source, and then edit the Excel file. This seems to be buggy, and graphs disappear and act in an unstable way.
So item 2 is the best way, to manage the charts in Excel and push them by copying/pasting them into the slides and locating them.
The trick is that pastespecial returns a shape range, not a shape, so the item(1) at the end returns the first item in the range. At least I KNOW it will be the first item since I am pasting my object into the slide.
Set oPPTSlide = oPPTFile.Slides(iSlideNum)
Set oPPTShape = oPPTSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile).Item(1)
oPPTShape.Left = -6
oPPTShape.Top = 164.25
We are unable to edit data underlying the charts. Check out the posting on an MSDN blog.
Key Differences Between the Chart object in Word 2007 SP2/PowerPoint 2007 SP2 and ChartObject object in Excel 2007 SP2
The Chart object in Word 2007 SP2 and PowerPoint 2007 SP2 does have a few differences from the Excel 2007 SP2 implementation.
* Programmatically creating or manipulating a ChartData object in Word 2007 SP2 or PowerPoint 2007 SP2 will cause Excel 2007 SP2 to run.
* Chart properties and methods for manipulating the chart sheet aren’t implemented.
The concept of a chart sheet is specific to Excel 2007. Chart sheets aren’t used in Word 2007 or PowerPoint 2007, so methods and properties used to reference or manipulate a chart sheet have been disabled for those applications.
* Properties and methods that, in Excel 2007 SP2 normally take a Range object reference now take a range address in Word 2007 SP2/PowerPoint 2007 SP2.
The Range object in Word 2007 SP2 and PowerPoint 2007 SP2 is different than the Range object in Excel 2007 SP2. To prevent confusion, the charting object model in Word 2007 SP2 and PowerPoint 2007 SP2 accepts range address strings, such as “=’Sheet1′!$A$1:$D$5″, in those properties and methods (such as the SetSourceData method of the Chart object) that accept Range objects in Excel 2007 SP2.
* A new object, ChartData, has been added to the VBA object models for Word 2007 SP2 and PowerPoint 2007 SP2 to provide access to the underlying linked or embedded data for a chart.
Each chart has, associated with it, the data used to draw the chart in Word 2007 SP2 or PowerPoint 2007 SP2. The chart data can either be linked from an external Excel workbook, or embedded as part of the chart itself. The ChartData object encapsulates access to the data for a given chart in Word 2007 SP2 or PowerPoint 2007 SP2. For example, the following VBA code displays, then minimizes, the chart data for each chart contained by the active document in Word 2007 SP2:
'This is a silly procedure that cycles through all the shapes on a slide 'and checks for the HTML <B> or <I> tags. When it finds them, it bolds 'or italics the text.



