我对Excel中的VBA非常陌生。我有一些图表在excel中,我想要自动粘贴到一个ppt模板。如图表1
和2
到幻灯片7
,图表3
和4
到幻灯片9
等等。
我设法打开了我的模板,但在复制图表时被卡住了。我是这样开始的,但它不起作用:
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
发布于 2021-06-21 10:32:21
我想你是早晚装订的。这个后期绑定代码对我来说很好。在代码的基础上添加Option Explicit
。看看会发生什么?
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库的引用。
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
https://stackoverflow.com/questions/68066101
复制相似问题