Question

I'm getting a "Object variable or With block variable not set" error in my code. This is my first crack at macro writing. I do have programming knowledge but this is new to me.

Anyway, I want to go through the presentation, and for every page that has any text in the notes section, I want to add a new slide (following it) containing that text.

Here's what I tried:

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

The line that gives the error is curSlide = ActivePresentation.Slides(i)

Was it helpful?

Solution

Use Set curSlide = ActivePresentation.Slides(i) - it's an object, and should be operated via Set.

OTHER TIPS

You need to use Set here, as you have with other objects:

Set curSlide = ActivePresentation.Slides(i)

Bingo. It's a bug in the Mac version of PowerPoint. I can repro the problem on the Mac.

.PlaceholderFormat.Type isn't supported on Mac PowerPoint, though it should be.

It's not 100% reliable, but you can pick up the second shape on the notes page as the body text placeholder instead:

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

I suspect you may also run into issues because you're looking at Slide.Count in the loop, but by adding slides, you're modifying Slide.Count.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top