【问题标题】:Export an Excel chart WITH DATA to PowerPoint with VBA使用 VBA 将带有数据的 Excel 图表导出到 PowerPoint
【发布时间】:2018-04-24 15:05:18
【问题描述】:

我已经有一个脚本可以复制图表:

'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 有关,但仅此而已。

【问题讨论】:

    标签: vba excel powerpoint


    【解决方案1】:

    如果您可以选择将图表发布为不再链接到 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"
    

    希望这会有所帮助!

    【讨论】:

    • 其实我就是从那里迁移过来的!这个想法是嵌入完全可编辑的图表。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2015-06-12
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多