【问题标题】:vba: powerpoint macro: "variable not set"vba:PowerPoint 宏:“未设置变量”
【发布时间】:2013-02-26 18:10:22
【问题描述】:

我的代码中出现"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)

【问题讨论】:

    标签: vba powerpoint powerpoint-2010


    【解决方案1】:

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

    【讨论】:

    • 谢谢!出于好奇,有什么更明智的方法来做我想做的事情吗?我想像这种计算效率这样的宏并不是特别重要。
    • 也许使用Ands 组合嵌套的Ifs。
    • 另外,在更正set命令后,我得到一个新的错误: PlaceholderFormat(unknown member): Failed For this line: If curShape.PlaceholderFormat.Type = ppPlaceholderBody Then
    • 您需要处理 Placeholders 集合中的特定项目。看到这个:msdn.microsoft.com/en-us/library/office/…
    • (不确定协议是什么...我要编辑我的原始帖子以显示新错误...还是开始一个新线程?)
    【解决方案2】:

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

    Set curSlide = ActivePresentation.Slides(i)
    

    【讨论】:

    • 你打败了我 31 秒))) +1
    【解决方案3】:

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

    .PlaceholderFormat.Type 在 Mac PowerPoint 上不受支持,但应该支持。

    这不是 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多