ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3574|回复: 4

PowerPoint2007如何用VBA粘贴Excel图表并包含完整工作簿?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-4-8 01:21 | 显示全部楼层 |阅读模式
本帖最后由 liucqa 于 2012-4-10 13:49 编辑

用ActivePresentation..Slides(1).Shapes.Paste 在2007上得到的是图表数据链接到Excel工作表

我想得到粘贴成完整工作薄的代码,如何写?

捕获.JPG

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-4-8 17:42 | 显示全部楼层
是不是要处理chartData对象呢,我看包含完整工作薄的shape的chart.chartdata对象,居然workbook不可用,那么图表的数据放到哪里去了?

附件的silde(1)就是带完整工作薄的shape,手工拷贝过来的。

test.rar (81.4 KB, 下载次数: 69)

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-4-8 18:11 | 显示全部楼层
Chart Object Model in Word & PowerPoint
by Diego M. Oppenheimer
on August 18

Over the next three posts this blog will cover some of the charting changes that have been made in Office 2010, many of which are the direct result of customer feedback.  

As part of the Office 2007 release we overhauled charting throughout Office; however an OM for Word and PowerPoint was not provided. In response to your feedback, in Office 2007 SP2 and Office 2010 we’ve exposed a unified/consistent charting OM within Word and PowerPoint, so that you can write solutions against any of the applications that take advantage of the new charting capabilities. The chart is drawn by the same shared Office drawing layer so if you are familiar with the OM in Excel 2007 you can easily create a similar solution in Word or PowerPoint.

Many times companies have a need to create charts in documents or presentations based on client specific data. In many cases these charts have a standard format, size and position which make them a prime candidate for automation. This OM can be used as a part of a solution to automate the generation of these charts which can save many hours of manual labor.

Let’s take a look at an example. Here is an example of how to create a chart in PowerPoint based on data your application has retrieved and apply standard formatting.

Sub CreateChart()

Dim myChart As Chart

Dim gChartData As ChartData

Dim gWorkBook As Excel.Workbook

Dim gWorkSheet As Excel.Worksheet
Set myChart = ActivePresentation.Slides(1).Shapes.AddChart.Chart 'create/set chart

Set gChartData = ActivePresentation.Slides(1).Shapes(1).Chart.ChartData 'Set chartdata

Set gWorkBook = gChartData.Workbook 'Set Workbook object reference

Set gWorkSheet = gWorkBook.Worksheets(1) 'Set Worksheet object reference
gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:B5") 'Add Data

gWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Sales"

gWorkSheet.Range("a2").Value = "Bikes"

gWorkSheet.Range("a3").Value = "Accessories"

gWorkSheet.Range("a4").Value = "Repairs"

gWorkSheet.Range("a5").Value = "Clothing"

gWorkSheet.Range("b2").Value = "1000"

gWorkSheet.Range("b3").Value = "2500"

gWorkSheet.Range("b4").Value = "4000"

gWorkSheet.Range("b5").Value = "3000"
With myChart 'Apply Style

    .ChartStyle = 4

    .ApplyLayout 4

    .ClearToMatchStyle

End With
myChart.HasTitle = True 'Add Title
With myChart.ChartTitle 'Format title

    .Characters.Font.Size = 18

    .Text = "2007 Sales"

End With
With myChart.Axes(xlValue) 'Add axis title

    .HasTitle = True

    .AxisTitle.Text = "$"

End With
myChart.ApplyDataLabels 'Add data labels
Set gWorkSheet = Nothing
gWorkBook.Application.Quit
Set gWorkBook = Nothing

Set gChartData = Nothing

Set myChart = Nothing
End Sub

As you can see a chart is still represented by a ChartObject and in this case is contained by a Shape. It can be contained by either an InlineShape or Shape in Word and a Shape in PowerPoint. From there the ChartObject is generally a mirror of what you would see in Excel. Some key differences are:

    Properties/Methods that normally accept a range object in Excel now accept a string of the range address in Word and PowerPoint.
    A new object, ChartData, has been added to Word and PowerPoint to access the underlying linked or embedded data for a chart.

The ability to programmatically manipulate charts in Word and PowerPoint also opens the door for a variety of interesting solutions around dynamically changing the appearance of your charts during a presentation or creating an interactive Word document. Here is an example of a solution to add data labels to a chart during a presentation:

Private Sub BtnDataLabels_Click()
Set myChart = ActivePresentation.Slides(1).Shapes(1).Chart 'Set chart object reference
myChart.ApplyDataLabels 'Add data labels
Set myChart = Nothing
End Sub

Try it out and let us know what you think!

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-4-17 08:52 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2012-8-5 17:47 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-24 02:35 , Processed in 0.030841 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表