如何从Excel复制图表并将其嵌入PPT?

时间:2014-11-18 20:47:54

标签: excel vba excel-vba charts

我尝试将粘贴图表从Excel复制到PPT,并将图表嵌入到PPT中。我使用以下代码:

Sub ChartToPresentation()
    ' Uses Early Binding to the PowerPoint Object Model
    ' Set a VBE reference to Microsoft PowerPoint Object Library
    Dim PPApp  As PowerPoint.Application
    Dim PPSlide As PowerPoint.Slide
    Dim PPPres As PowerPoint.Presentation

    ' Reference existing instance of PowerPoint
    Set PPApp = GetObject(, "Powerpoint.Application")

    ' Reference active presentation
    Set PPPres = PPApp.ActivePresentation
    PPApp.ActiveWindow.ViewType = ppViewSlide

    'Copy "Chart 2" to from "Sheet3" to Slide # 10
    ' Copy "Chart 1" on "Sheet3" as a picture
    ActiveWorkbook.Sheets("Sheet3").ChartObjects("Chart 2").Copy

    ' Paste chart  to Slide # 10
    With PPPres.Slides(10).Shapes.PasteSpecial(DataType:=ppPasteOLEObject, _
       Link:=msoTrue)
        ' Align pasted chart
        .Align msoAlignCenters, True
        .Align msoAlignMiddles, True
    End With

    ' Clean up
    Set PPSlide = Nothing
    Set PPPres = Nothing
    Set PPApp = Nothing

    AppActivate ("Microsoft PowerPoint")
End Sub

当我运行此操作时,我收到此错误:Run time error. Shapes (unknown member):Invalid request. The specific data type is unavailable

在线:With PPPres.Slides(10).Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoTrue)

我不确定为什么会收到此错误。我指定的数据类型是PasteSpecial的数据类型之一。如何解决此问题并将图表粘贴为嵌入式图表。

提前致谢! :)

1 个答案:

答案 0 :(得分:1)

您尝试复制的内容实际上不是图表,而是图表对象的图表区域
所以尝试类似的事情:

ActiveWorkbook.Sheets("Sheet3").ChartObjects("Chart 2").ChartArea.Copy
With PPPres.Slides(10).Shapes.PasteSpecial(DataType:=ppPasteOLEObject, Link:=msoTrue)
    '~~> Rest of your code here
End With

这是你的尝试吗? HTH。