首页 > 解决方案 > VBA-自动将图表和表格复制到ppt

问题描述

我正在编写一些代码来自动将一些图表从excel复制到ppt。我面临的第一个问题是变量声明

Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide

错误是“未定义用户定义类型”。只是为了让你知道我对这个 VBA 很陌生,所以一些描述性的评论会很有帮助。

标签: excelvbachartspowerpoint

解决方案


您得到的错误是因为您的 powerpoint 变量希望被定义为对象,并且稍后将对象设置为 powerpoint 应用程序。

Sub ChartX2P()

Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object

If ActiveChart Is Nothing Then 
MsgBox "Hey, please select a chart first." 
Exit Sub 
End If

If PowerPointApp Is Nothing Then _ 
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")

On Error GoTo 0

Application.ScreenUpdating = False

'this will create a new powerpoint for your chart
Set myPresentation = PowerPointApp.Presentations.Add

'this will open an old powerpoint up, just change "File address" to the address
'Set myPresentation = PowerPointApp.Presentations.Open(Filename:="FileAddress")

Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly

ActiveChart.ChartArea.Copy

mySlide.Shapes.Paste
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

myShape.Left = 200
myShape.Top = 200

PowerPointApp.Visible = True
PowerPointApp.Activate

Application.CutCopyMode = False

End Sub

这是从这里获取的,但它看起来很简单,如果您无法从该站点获得任何信息,请告诉我


推荐阅读