Excel VBA 中的幻灯片和形状对象错误
Errors with Slide and Shape Objects in Excel VBA
我正在尝试使用 Excel 中的 VBA 检索 PowerPoint 连接到的链接。我将在下面附加的两种不同方法中收到两个不同的错误,这两种错误都源于调用 PowerPoint 的 Slide 和 Shape 对象。第一个宏导致“需要对象”错误,从 For 循环的第一行开始。
Sub Macro1()
'Opening up the PowerPoint to retrieve the links
Dim PPTName As String
Dim PPTApp As Object
PPTName = Sheets("Sheet1").Range("G2").Value
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Presentations.Open PPTName
Dim i As Integer
Dim j As Long
Dim k As Long
i = 10
For j = 1 To PPT.ActivePresentation.Slides.Count
For k = 1 To PPT.ActivePresentation.Slides(i).Shapes.Count
If PPTShape.Type = msoLinkedPicture Or PPTShape.Type = msoLinkedOLEObject Then
Sheets("Sheet1").Range("G" & CStr(i)) = PPTShape.LinkFormat.SourceFullName
i = i + 1
End If
k = k + 1
Next k
j = j + 1
Next j
End Sub
第二个宏导致“编译错误”,从“Set PPTSlides = CreateObject("PowerPoint.Slides")”开始。“
Sub Macro2()
Dim PPTName As String
Dim PPTApp As Object
PPTName = Sheets("Sheet1").Range("G2").Value
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Presentations.Open PPTName
Dim PPTSlides As Object
Dim PPTShapes As Object
Set PPTSlides = CreateObject("PowerPoint.Slides")
Set PPTShapes = CreateObject("PowerPoint.Shapes")
For Each PPTSlides In PPT.ActivePresentation.Slides
For Each PPTShapes In PPT.ActivePresentation.Shapes
If PPTShape.Type = msoLinkedPicture Or PPTShape.Type = msoLinkedOLEObject Then
Sheets("Sheet1").Range("G" & CStr(i)) = PPTShape.LinkFormat.SourceFullName
i = i + 1
End If
Next PPTShapes
Next PPTSlides
End Sub
我之前没有在 Excel 中使用过 VBA 来使用 PowerPoint,所以这对我来说是一个新的学习曲线。由于这些错误,我也无法检查我的 For 循环是否有错误。在这些问题上的任何帮助表示赞赏。提前致谢!
幸运的是,这只是一个小问题:使用了错误的索引:
i = 10
For j = 1 To PPT.ActivePresentation.Slides.Count
For k = 1 To PPT.ActivePresentation.Slides(i).Shapes.Count
如果仔细看的话,最后一行需要用j
代替i
对于第二个代码清单,您可以省略行
Set PPTSlides = CreateObject("PowerPoint.Slides")
Set PPTShapes = CreateObject("PowerPoint.Shapes")
因为下面第一个变量将从 ActivePresentation.Slides
.
开始设置
当您使用 for each
循环时,将这两个变量从复数重命名为单数也很有意义,即 PPTSlide
而不是 PPTSlides
.
另请注意,For Each PPTShapes In PPT.ActivePresentation.Shapes
不起作用。您需要从 For Each PPTShape in PPTSlide.Shapes
.
获取形状
祝一切顺利
我正在尝试使用 Excel 中的 VBA 检索 PowerPoint 连接到的链接。我将在下面附加的两种不同方法中收到两个不同的错误,这两种错误都源于调用 PowerPoint 的 Slide 和 Shape 对象。第一个宏导致“需要对象”错误,从 For 循环的第一行开始。
Sub Macro1()
'Opening up the PowerPoint to retrieve the links
Dim PPTName As String
Dim PPTApp As Object
PPTName = Sheets("Sheet1").Range("G2").Value
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Presentations.Open PPTName
Dim i As Integer
Dim j As Long
Dim k As Long
i = 10
For j = 1 To PPT.ActivePresentation.Slides.Count
For k = 1 To PPT.ActivePresentation.Slides(i).Shapes.Count
If PPTShape.Type = msoLinkedPicture Or PPTShape.Type = msoLinkedOLEObject Then
Sheets("Sheet1").Range("G" & CStr(i)) = PPTShape.LinkFormat.SourceFullName
i = i + 1
End If
k = k + 1
Next k
j = j + 1
Next j
End Sub
第二个宏导致“编译错误”,从“Set PPTSlides = CreateObject("PowerPoint.Slides")”开始。“
Sub Macro2()
Dim PPTName As String
Dim PPTApp As Object
PPTName = Sheets("Sheet1").Range("G2").Value
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Presentations.Open PPTName
Dim PPTSlides As Object
Dim PPTShapes As Object
Set PPTSlides = CreateObject("PowerPoint.Slides")
Set PPTShapes = CreateObject("PowerPoint.Shapes")
For Each PPTSlides In PPT.ActivePresentation.Slides
For Each PPTShapes In PPT.ActivePresentation.Shapes
If PPTShape.Type = msoLinkedPicture Or PPTShape.Type = msoLinkedOLEObject Then
Sheets("Sheet1").Range("G" & CStr(i)) = PPTShape.LinkFormat.SourceFullName
i = i + 1
End If
Next PPTShapes
Next PPTSlides
End Sub
我之前没有在 Excel 中使用过 VBA 来使用 PowerPoint,所以这对我来说是一个新的学习曲线。由于这些错误,我也无法检查我的 For 循环是否有错误。在这些问题上的任何帮助表示赞赏。提前致谢!
幸运的是,这只是一个小问题:使用了错误的索引:
i = 10
For j = 1 To PPT.ActivePresentation.Slides.Count
For k = 1 To PPT.ActivePresentation.Slides(i).Shapes.Count
如果仔细看的话,最后一行需要用j
代替i
对于第二个代码清单,您可以省略行
Set PPTSlides = CreateObject("PowerPoint.Slides")
Set PPTShapes = CreateObject("PowerPoint.Shapes")
因为下面第一个变量将从 ActivePresentation.Slides
.
当您使用 for each
循环时,将这两个变量从复数重命名为单数也很有意义,即 PPTSlide
而不是 PPTSlides
.
另请注意,For Each PPTShapes In PPT.ActivePresentation.Shapes
不起作用。您需要从 For Each PPTShape in PPTSlide.Shapes
.
祝一切顺利