使用VBA将带有DATA的Excel图表导出到PowerPoint

时间:2018-04-23 23:20:47

标签: excel vba excel-vba powerpoint powerpoint-vba

我已经有一个脚本只是复制图表:

'Do the following:

Set wrkbk = ActiveWorkbook
' Load up the PowerPoint template
Set objPPT = CreateObject("Powerpoint.application")
objPPT.Visible = True
objPPT.Presentations.Open Filename:="path\to\template.pptx", ReadOnly:=msoTrue
objPPT.ActiveWindow.ViewType = 1 'ppViewSlide


' Variable defs
Dim wrksht As Worksheet
Dim chart As Object
Dim pastedChart As Object

' We're only going to copy from the current worksheet
Set wrksht = ActiveSheet
Set chart = wrksht.ChartObjects(1).chart ' We'll just use the default chart
' Now we select and copy the chart...
chart.ChartArea.Select
chart.ChartArea.Copy
With Selection
    ' Open up the active slide and paste into it
    Set pastedChart = objPPT.ActiveWindow.View.Slide.Shapes.Paste
    ' ' In earlier versions of excel, we needed to be a lot more specific ...
    ' objPPT.ActiveWindow.View.Slide.Shapes.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
    '      Placement:=wdInLine, DisplayAsIcon:=False
    ' The default paste is a little overscaled. Scale it down.
    ' See:
    ' https://msdn.microsoft.com/en-us/VBA/PowerPoint-VBA/articles/shape-scaleheight-method-powerpoint
    pastedChart.ScaleHeight 0.9, msoFalse, msoScaleFromMiddle
 End With

这很好用,我也有一个脚本可以将它推广到工作簿中的所有工作表。

但是,粘贴的图表对象仍引用源Excel文件。显然,PowerPoint中可以完全包含图表(因为您只能在那里创建图表);问题是,我可以这样做并使PPTX自成一体吗?

我最好的猜测是它与the DataTable attribute有关,但就是这个。

1 个答案:

答案 0 :(得分:0)

如果将图表作为未链接到Excel的图片发布为您的选项,则可以对导出的图表执行以下操作:插入图片 - >导出图表后链接到PowerPoint中的文件:

Dim strFilePath, strFileName As String
Dim ChrtObj             As ChartObject

strFilePath = "path\to\template\"
strFileName = "Chart.png"  'your file name here
Set ChrtObj = Worksheets("YourSheet'sName").ChartObjects(1)
ChrtObj.Activate
ChrtObj.Chart.Export Filename:=strFilePath & strFileName, Filtername:="PNG"

希望这有帮助!