首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将excel图表粘贴到电源点模板中

将excel图表粘贴到电源点模板中
EN

Stack Overflow用户
提问于 2021-06-21 10:15:31
回答 1查看 95关注 0票数 0

我对Excel中的VBA非常陌生。我有一些图表在excel中,我想要自动粘贴到一个ppt模板。如图表12到幻灯片7,图表34到幻灯片9等等。

我设法打开了我的模板,但在复制图表时被卡住了。我是这样开始的,但它不起作用:

代码语言:javascript
运行
复制
Sub CreatePowerPointTemplate()
    Dim PowerPointApp As PowerPoint.Application
    Dim cht As Excel.ChartObject

    Set PowerPointApp = CreateObject("PowerPoint.Application")

    strpath = ThisWorkbook.Path & "\My_template.pptx"
    PowerPointApp.Presentations.Open (strpath)

    ' get chart #5 and copy it into slide #7
    Set cht = Worksheets("Graphs").ChartObjects(5)
    cht.Copy
    PowerPointApp.ActivePresentation.Slides(7).Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-21 10:32:21

我想你是早晚装订的。这个后期绑定代码对我来说很好。在代码的基础上添加Option Explicit。看看会发生什么?

代码语言:javascript
运行
复制
Option Explicit

'~~> If you are using late binding then you need to specify the value
'~~> of the power point constant without which Excel would not know
'~~> what this is...
Private Const ppPasteMetafilePicture As Integer = 3

Sub CreatePowerPointTemplate()
    Dim PowerPointApp As Object
    Dim PowerPointPrsn As Object
    Dim cht As ChartObject
    Dim strpath As String
    
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    strpath = ThisWorkbook.Path & "\My_template.pptx"
    Set PowerPointPrsn = PowerPointApp.Presentations.Open(strpath)

    ' get chart #5 and copy it into slide #7
    Set cht = Worksheets("Graphs").ChartObjects(5)
    cht.Copy
    DoEvents '<~~ Give time to excel to place the object in the clipboard
    
    PowerPointPrsn.Slides(7).Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
End Sub

下面是一个早期绑定示例。您需要通过Tools->References设置对Microsoft Powerpoint对象xx.xx库的引用。

代码语言:javascript
运行
复制
Option Explicit

Sub CreatePowerPointTemplate()
    Dim PowerPointApp As PowerPoint.Application
    Dim PowerPointPrsn As PowerPoint.Presentation
    Dim cht As ChartObject
    Dim strpath As String
    
    strpath = ThisWorkbook.Path & "\My_template.pptx"
    
    Set PowerPointApp = New PowerPoint.Application
    Set PowerPointPrsn = PowerPointApp.Presentations.Open(strpath)

    ' get chart #5 and copy it into slide #7
    Set cht = Worksheets("Graphs").ChartObjects(5)
    cht.Copy
    DoEvents
    
    PowerPointPrsn.Slides(7).Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68066101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档