VBA把excel中的图表复制到PPT中
时间: 2023-09-20 10:09:12 浏览: 225
将Excel表格嵌入PPT中.pptx
5星 · 资源好评率100%
可以使用VBA代码将Excel中的图表复制到PPT中。以下是示例代码:
```
Sub CopyChartsToPowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShape As PowerPoint.Shape
Dim myChart As ChartObject
' Create a new instance of PowerPoint
Set pptApp = New PowerPoint.Application
' Open an existing presentation or create a new one
Set pptPres = pptApp.Presentations.Open("C:\Presentation1.pptx")
' Loop through all the ChartObjects in the active sheet
For Each myChart In ActiveSheet.ChartObjects
' Copy the chart to the clipboard
myChart.Copy
' Add a new slide to the presentation
Set pptSlide = pptPres.Slides.Add(pptPres.Slides.Count + 1, ppLayoutBlank)
' Paste the chart onto the slide
Set pptShape = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
' Set the position and size of the chart on the slide
pptShape.Left = 50
pptShape.Top = 50
pptShape.Width = 500
pptShape.Height = 300
Next myChart
' Save and close the presentation
pptPres.Save
pptPres.Close
' Quit PowerPoint
pptApp.Quit
' Clean up
Set pptShape = Nothing
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
```
这段代码会遍历当前活动工作表中的所有图表,并将它们复制到一个新的幻灯片中。你可以根据需要自定义位置和大小。
请注意,在运行此代码之前,你需要在VBA编辑器中启用对PowerPoint对象模型的引用。这可以通过选择工具->引用菜单,然后勾选“Microsoft PowerPoint XX.0 Object Library”来完成,其中XX是你安装的PowerPoint版本号。
阅读全文