VBA-自动将图表和表格复制到ppt

时间:2018-05-10 06:33:34

标签: excel vba charts powerpoint

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

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

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

1 个答案:

答案 0 :(得分:0)

您获得的错误是因为您的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

这取自here,但它看起来非常简单,您无法从网站获取任何内容让我知道