我正在尝试使用VBA将数据从excel复制到powerpoint。我有以下代码,我认为应该可以工作,但即使我已经声明并具体说明所有变量,它仍然会给我一个错误。
dosync
似乎不喜欢这条线
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
知道如何解决这个问题吗?当我尝试运行它时,我收到错误:
运行时错误'91':
对象变量或未设置块变量
答案 0 :(得分:1)
首先,我强烈建议您阅读Early and late binding
在尝试打开演示文稿之前,您必须先创建PowerPoint应用程序的新实例。
这应该有效:
'your code
Set PowerPointApp = New PowerPoint.Application
'the rest of your code
'Open Powerpoint
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)