使用excel vba更新powerpoint中的现有嵌入式图表

时间:2016-10-10 19:25:05

标签: excel vba powerpoint-vba

我在powerpoint中插入了一个图表。我正在使用它作为模板。我想用excel表中的数据编辑该图表的数据。这个

有一个excel vba代码吗?

3 个答案:

答案 0 :(得分:3)

PowerPoint中的任何图表(有例外情况,可以“破坏”现有图表,但这不在范围内,此处)具有ChartData属性,该属性返回包含数据的Excel工作簿图表。

当使用“模板”幻灯片工作时,应该可以安全地假设图表数据存在于工作表1和ListObject表中(工作表中应该只有一个这样的表)。 / p>

在PowerPoint VBA中,需要引用Excel对象库,这将向您展示如何获取包含图表数据的ListObject的句柄:

Sub ShowChartData()
Dim sld As Slide
Dim shp As Shape
Dim cht As Chart
Dim chtData As ChartData
Dim cTable As Excel.ListObject

'Assume we have only one slide, at slide 1:
Set sld = ActivePresentation.Slides(1)
'Assume the Chart is the second shape, modify if needed
Set shp = sld.Shapes(2)    
'Handle the chart
Set cht = shp.Chart
'Handle the CharttData
Set chtData = cht.ChartData

'Open & minimize the ChartData, you don't need to see it, but it must be OPEN to edit it
chtData.Activate
chtData.Workbook.Application.WindowState = -4140

With chtData
    Set cTable = chtData.Workbook.Worksheets(1).ListObjects(1)

    ' Here, you can update the ListObject in the same ways you
    ' would do so in Excel, natively.

End With

'Remember to close the workbook
chtData.Workbook.Close

End Sub

现在您已经掌握了ListObject,您需要以某种方式获取来自Excel的值。

这将需要处理Excel.Application类的打开实例(或提示用户从FileDialog中选择文件等)并识别要放入PowerPoint中的数据,以及如何安排它。通常可以通过将Excel中的值转储到变量数组中并将其传递给PowerPoint来完成。

由于这些都是你省略的细节,请注意我绝对不愿意接受可能是一个永无止境的系列节目“但我怎么做这样的......?”当你了解自己的逻辑和用例要求的复杂性时,后续问题。

以上代码旨在从PowerPoint执行。如果你需要从 Excel中运行,它将需要不同的代码(未经测试,但是类似这样的东西)。

Sub ShowPPTChartData()
' to be run from Excel VBA
'Requires reference to PowerPoint library
Dim ppt as PowerPoint.Application
Dim pres as PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim cht As PowerPoint.Chart
Dim chtData As PowerPoint.ChartData
Dim cTable As Excel.ListObject

Set ppt = GetObject(,"PowerPoint.Application")
'Assume we have only one open Presentation file:
Set pres = ppt.Presentations(1)
'Assume we have only one slide, at slide 1:
Set sld = pres.Slides(1)
'Assume the Chart is the second shape, modify if needed
Set shp = sld.Shapes(2)    
'Handle the chart
Set cht = shp.Chart
'Handle the CharttData
Set chtData = cht.ChartData

'Open & minimize the ChartData, you don't need to see it, but it must be OPEN to edit it
chtData.Activate
chtData.Workbook.Application.WindowState = -4140

With chtData
    Set cTable = chtData.Workbook.Worksheets(1).ListObjects(1)

    ' Here, you can update the ListObject in the same ways you
    ' would do so in Excel, natively.

End With

'Remember to close the workbook
chtData.Workbook.Close

End Sub

编辑可以在没有激活 ChartData.Workbook的情况下编辑现有图表,如下所示:

Update PowerPoint chart without opening chart workbook or making it invisible

从图表中添加/删除系列比操纵已经属于图表系列的数据更为棘手。

答案 1 :(得分:0)

Set Current_Chart_Shape = Active_Slide.Shapes("Monthly Chart")
Set Current_Chart = Current_Chart_Shape.Chart
Set Current_Chart_Data = Current_Chart.ChartData

Current_Chart_Data.Activate
Set PPTChartSheet = Current_Chart.ChartData.Workbook.Sheets(1)
Current_Chart_Data.Workbook.Application.WindowState = -4140

With PPTChartSheet
.ListObjects("Table1").Resize PPTChartSheet.Range("A1:C8")
For l = 0 To 6
.Range("B2").Offset(l, 0).Value = Array_Values(l)
Next l
End With

Current_Chart_Data.Workbook.Close

答案 2 :(得分:-1)

你不需要做任何特别的事。

嵌入图表并链接到Excel工作表后,即使PowerPoint文件关闭,PowerPoint也不会在何时或如何更新它。 基本上,无论您对该工作表做什么,VB或手动操作,都会在下次打开时反映在PowerPoint中。