vba:powerpoint宏:“变量未设置”

时间:2013-02-26 18:10:22

标签: vba powerpoint powerpoint-vba powerpoint-2010

我的代码中出现"Object variable or With block variable not set"错误。 这是我写宏观的第一个问题。我确实有编程知识,但这对我来说是新的。

无论如何,我想通过演示文稿,对于笔记部分中包含任何文本的每个页面,我想添加一个包含该文本的新幻灯片(包括它)。

这是我试过的:

Sub SlideSort()
Dim curSlide As Slide
Dim newSld As Slide
Dim curPres As Presentation
Dim curShape As Shape
Dim i As Integer

    For i = 1 To ActivePresentation.Slides.Count
        curSlide = ActivePresentation.Slides(i)

        For Each curShape In curSlide.NotesPage.Shapes
            If curShape.Type = msoPlaceholder Then
                If curShape.PlaceholderFormat.Type = ppPlaceholderBody Then
                    If curShape.TextFrame.TextRange <> "" Then
                        Set newSld = ActivePresentation.Slides.Add(Index:=i + 1, Layout:=ppLayoutText)
                        newSld.Shapes(2).TextFrame.TextRange = curShape.TextFrame.TextRange
                        i = i + 1

                    End If
                End If
            End If
        Next curShape
    Next i

End Sub

给出错误的行是curSlide = ActivePresentation.Slides(i)

3 个答案:

答案 0 :(得分:3)

使用Set curSlide = ActivePresentation.Slides(i) - 它是对象,应该通过Set进行操作。

答案 1 :(得分:1)

您需要使用Set here,就像使用其他对象一样:

Set curSlide = ActivePresentation.Slides(i)

答案 2 :(得分:0)

宾果。这是Mac版PowerPoint中的一个错误。我可以在Mac上重现这个问题。

Mac计划不支持.PlaceholderFormat.Type,但应该是。

它不是100%可靠,但您可以在备注页面上选取第二个形状作为正文文本占位符:

Sub SlideSort()
Dim curSlide As Slide
Dim newSld As Slide
Dim curPres As Presentation
Dim curShape As Shape
Dim i As Integer

    For i = 1 To ActivePresentation.Slides.Count
        curSlide = ActivePresentation.Slides(i)
        curShape = curSlide.NotesPage.Shapes(2)
           If curShape.TextFrame.TextRange <> "" Then
              Set newSld = ActivePresentation.Slides.Add(Index:=i + 1, Layout:=ppLayoutText)
              newSld.Shapes(2).TextFrame.TextRange = curShape.TextFrame.TextRange
              i = i + 1
           End If
    Next i

End Sub

我怀疑你也可能遇到问题,因为你在循环中查看Slide.Count,但是通过添加幻灯片,你正在修改Slide.Count。