我正在尝试使用VBA将数据从excel复制到PowerPoint。我有以下代码,我相信它应该有效,但即使我已经声明并指定了所有变量,它也一直给我一个错误。
Sub CopyToPPT()
Dim DestinationPPT As String
Dim rng As Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As Object
Dim myShape As Object
Dim myShapeRange As Range
DestinationPPT = "C:powerpoint.pptx"
'Open Powerpoint
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Set rng = ThisWorkbook.ActiveSheet.Range("B2:D14")
Set mySlide = myPresentation.Slides(5)
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
'Copy
rng.copy
'Paste
mySlide.Shapes.PasteSpecial DataType:=2
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShapeRange.Left = 234
myShapeRange.Top = 186
End Sub
它似乎不喜欢这条线
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
知道我该如何解决这个问题吗?当我尝试运行它时,出现错误:
运行时错误"91":
对象变量或未设置块变量
首先,我强烈建议阅读有关早期和晚期绑定的信息
在尝试打开演示文稿之前,您必须创建 PowerPoint 应用程序的新实例。
这应该有效:
'your code
Set PowerPointApp = New PowerPoint.Application
'the rest of your code
'Open Powerpoint
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)