简体   繁体   English

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

[英]Export an Excel chart WITH DATA to PowerPoint with VBA

I already have a script going to just copy a chart: 我已经有一个脚本可以复制图表:

'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

This works just fine, and I also have a script that generalizes this to all worksheets in a workbook. 这工作得很好,并且我还有一个脚本可以将其推广到工作簿中的所有工作表。

However, the pasted chart object still references the source Excel file. 但是,粘贴的图表对象仍引用源Excel文件。 Obviously, a chart can be fully contained in PowerPoint (since you can create charts solely in there); 显然,图表可以完全包含在PowerPoint中(因为您可以仅在其中创建图表)。 the question is, can I do this and make the PPTX self-contained? 问题是,我可以这样做并使PPTX独立吗?

My best guess is that it is related to the DataTable attribute , but that's about it. 我最好的猜测是,它与DataTable属性有关 ,但仅此而已。

If publishing the chart as a picture which is not linked to your Excel any longer is an option for you, you might do the following with your exported chart: Insert picture -> link to file in your PowerPoint after exporting the chart like this: 如果将图表发布为不再与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"

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM