2013-02-26 48 views
1

我在我的代码中出现"Object variable or With block variable not set"错误。 这是我在宏观写作方面的第一个破解。我有编程知识,但这对我来说是新的。vba:powerpoint宏:“变量未设置”

无论如何,我想要通过演示文稿,并且对于每个在备注部分有任何文本的页面,我都想添加一个包含该文本的新幻灯片(在其后面)。

这里是我的尝试:

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

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

+0

谢谢!出于好奇,有什么更明智的方式去做我想做的事情? 我猜想宏这样的计算效率并不是特别的本质。 – JoshDG 2013-02-26 19:56:34

+0

也许结合使用'和'的嵌套'If's。 – 2013-02-26 19:58:29

+0

此外,修正set命令后,我得到了一个新的错误:PlaceholderFormat(未知成员):失败 对于此行:如果curShape.PlaceholderFormat.Type = ppPlaceholderBody然后 – JoshDG 2013-02-26 20:01:05

1

你需要使用此设置,因为你与其他对象:

Set curSlide = ActivePresentation.Slides(i) 
+0

你打我31秒)))+1 – 2013-02-26 18:36:19

0

宾果。这是Mac版PowerPoint中的一个错误。我可以在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。