Share via

In PowerPoint VBA, how do I refer to the ACTIVE slide?

Anonymous
2012-02-09T21:27:55+00:00

I want to be able to insert an object on the active slide in VBA code. I could only find

ActivePresentation.Slides(2).Shapes.Addshape...

like I have to refer to a particular slide rather than the active one. I'm sure it can be done, but couldn't find it by browsing the object browser.

TIA

Bob Umlas

Excel MVP

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. HansV 462.6K Reputation points MVP Volunteer Moderator
    2012-02-09T21:56:59+00:00

    Use code like this:

    Dim sld As Slide

    Dim shp As Shape

    Set sld = Application.ActiveWindow.View.Slide

    Set shp = sld.Shapes.AddShape(Type:=msoShapeRectangle, _

        Left:=50, Top:=50, Width:=100, Height:=200)

    shp.Fill.ForeColor.RGB = vbBlue

    40+ people found this answer helpful.
    0 comments No comments

12 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-11-08T12:37:31+00:00

    The original code is for use in normal edit view. It sounds like you are in show view?

    Try:

    Sub getCurrent()

    Dim osld As Slide

    Set osld = ActivePresentation.SlideShowWindow.View.Slide

    MsgBox "The current slide is slide " & osld.SlideIndex

    End Sub

    10 people found this answer helpful.
    0 comments No comments
  2. Steve Rindsberg 99,161 Reputation points MVP Volunteer Moderator
    2012-02-10T17:09:56+00:00

    I'd add that you want a little error trapping.  There are a few oddball situations where this wlll throw errors (e.g. in slide sorter view, when the insertion cursor is between slides, no slide actually selected).

    And if the user's in Notes Page view, this will put the rectangle on the notes page, not on the slide itself.  You can check the view type and set it to something else if need be:

    With ActiveWindow

        If .View.Type = ppViewNotesPage Then

            .ViewType = ppViewSlide

        End If

    End With

    And out of politeness, you'd want to record the original view type and reset it once you're done.

    Or better yet, just reference the slide directly:

    Dim sld As Slide

    Dim shp As Shape

    Dim SlideIndex As Long

    SlideIndex = ActiveWindow.View.Slide.SlideIndex

    Set sld = ActivePresentation.Slides(SlideIndex)

    Set shp = sld.Shapes.AddShape(Type:=msoShapeRectangle, _

        Left:=50, Top:=50, Width:=100, Height:=200)

    shp.Fill.ForeColor.RGB = vbBlue

    5 people found this answer helpful.
    0 comments No comments
  3. HansV 462.6K Reputation points MVP Volunteer Moderator
    2012-02-10T17:13:42+00:00

    Thanks, Steve!

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2012-02-09T22:18:03+00:00

    Thanks! Say hi at the Summit if you're going!

    1 person found this answer helpful.
    0 comments No comments