VBA Code


SlideRange Collection

A collection that represents a notes page or a slide range, which is a set of slides that can contain as little as a single slide or as much as all the slides in a presentation.
You can include whichever slides you wantó chosen from all the slides in the presentation or from all the slides in the selectionó to construct a slide range.
For example, you could construct a SlideRange collection that contains the first three slides in a presentation, all the selected slides in the presentation, or all the title slides in the presentation.


Use Slides.Range(index), where index is the name or index number of the slide or an array that contains either names or index numbers of slides, to return a SlideRange collection that represents a set of slides in a presentation. You can use the Array function to construct an array of names or index numbers. The following example sets the background fill for slides one and three in the active presentation.


Use the SlideRange property of the Selection object to return all the slides in the selection. The following example sets the background fill for all the selected slides in window one, assuming that there's at least one slide selected.


Use Selection.SlideRange(index), where index is the slide name or index number, to return a single slide from the selection. The following example sets the background fill for slide two in the collection of selected slides in window one, assuming that there are at least two slides selected.


Use the NotesPage property to return a SlideRange collection that represents the specified notes page. The following example inserts text into placeholder two (the notes area) on the notes page for slide one in the active presentation.


Just as you can work with several slides at the same time in the user interface by selecting them and applying a command, you can work with several slides at the same time programmatically by constructing a SlideRange collection and applying properties or methods to it. And just as some commands in the user interface that work on single slides aren't valid when multiple slides are selected, some properties and methods that work on a Slide object or on a SlideRange collection that contains only one slide will fail if they're applied to a SlideRange collection that contains more than one slide. In general, if you can't do something manually when more than one slide is selected (such as return the individual shapes on one of the slides), you can't do it programmatically by using a SlideRange collection that contains more than one slide.


For those operations that work in the user interface whether you have a single slide or multiple slides selected (such as copying the selection to the Clipboard or setting the slide background fill), the associated properties and methods will work on a SlideRange collection that contains more than one slide. Here are some general guidelines for how these properties and methods behave when they're applied to multiple slides.


Applying a method to a SlideRange collection is equivalent to applying the method to all the Slide objects in that range as a group.
Setting the value of a property of the SlideRange collection is equivalent to setting the value of the property in each slide in that range individually (for a property that takes an enumerated type, setting the value to the "Mixed" value has no effect).
A property of the SlideRange collection that returns an enumerated type returns the value of the property for an individual slide in the collection if all slides in the collection have the same value for that property. If the slides in the collection don't all have the same value for the property, the property returns the "Mixed" value.
A property of the SlideRange collection that returns a simple data type (such as Long, Single, or String) returns the value of the property for an individual slide in the collection if all slides in the collection have the same value for that property. If the slides in the collection don't all have the same value for the property, the property will return - 2 or generate an error. For example, using the Name property on a SlideRange object that contains multiple slides will generate an error because each slide has a different value for its Name property.
Some formatting properties of slides aren't set by properties and methods that apply directly to the SlideRange collection, but by properties and methods that apply to an object contained in the SlideRange collection, such as the ColorScheme object. If the contained object represents operations that can be performed on multiple objects in the user interface, you'll be able to return the object from a SlideRange collection that contains more than one slide, and its properties and methods will follow the preceding rules. For example, you can use the ColorScheme property to return the ColorScheme object that represents the color schemes used on all the slides in the specified SlideRange collection. Setting properties for this ColorScheme object will also set these properties for the ColorScheme objects on all the individual slides in the SlideRange collection.



Selecting / Activating Slides

Slides start from the number 1

ActivePresentation.Slides(1).Select 

Returns the total number of slides

ActivePresentation.Slides.Count 


Inserting Slides

Insert a slide after the curren slide 
Dim objView As Presentation.View
With ActivePresentation.Slides
   Set objView = ActiveWindow.View
   objView.GotoSlide.Add(objView.Slide.SlideIndex + 1, ppLayoutTitleOnly).SlideIndex
   Set objView = Nothing
End With


Dim i As Integer, Sl As Slide 
 
With ActivePresentation
'If there is at least one slide.
    If .Slides.Count >= 1 Then
         
'One way to loop
        For i = 1 To .Slides.Count
'Do stuff, for instance :
            MsgBox .Slides(i).SlideID
        Next i
         
'And an other one...
        For Each Sl In .Slides
'Do stuff, for instance :
            MsgBox Sl.SlideID
        Next Sl
         
    End If
End With


If we would like to manipulate some or all slides of a presentation in one go, a loop would not be the most efficient way, the "Range" method would be a much better one.


Manipulating specific slides in one go (change their "Background" colour and effect):

 'Create a range with slides 2, 3 and 4.
With ActivePresentation.Slides.Range(Array(2, 3, 4))
'Ignore the default background settings.
    .FollowMasterBackground = False
'And add a new background color and effect.
    .Background.Fill.PresetGradient msoGradientHorizontal, 1, msoGradientDaybreak
End With


Manipulating all slides in one go (change their "SlideShowTransition" settings):

With ActivePresentation.Slides.Range.SlideShowTransition 
'make sure the next slide will be shown automatically.
    .AdvanceOnTime = True
'Set "advance to next slide" time (in seconds).
    .AdvanceTime = 5
End With


Note that not all properties can be changed and not all methods can be applied on an entire presentation or a multiple sliderange!


Identifying slides: names, numbers and indices...
Each slide has a "SlideID", "SlideIndex" and "SlideNumber".


The slide ID is a unique number added by default to each slide you add to a presentation. This number will never change during the lifetime of the slide, nor by moving the slide within the presentation, as long as the slide stays in the presentation in which it was created.


The slide index is the number which represents the slide's current position within the presentation, and will change as soon the slide is moved within the presentation. Needless to say the movement of a slide within a presentation will also affect the index number of some or all other slides in the same presentation, dependent on the position the slide had in the slides hierarchy before it was moved. For instance, moving the first slide in a presentation after the 5th, will also change the index number of 4 other slides.


The slide number is added by default, like the slide ID, but its use is mostly for printing purposes only, and it can be hidden or made visible in a slide's header or footer. By default the slide number will correspond with the slide index number, unless we change the "FirstSlideNumber", code wise or manually (File > Page Setup > Number slides from). The slide number will always be equal to the starting slide number + the slide index number - 1.


We can determine which slide we are looking at (no matter whether the presentation is running as a slide show or in edit mode), with a piece of code which looks like this:


Dim i As Integer, SlIndex As Integer 
 
With ActivePresentation
'Check if any slide shows are running.
    If SlideShowWindows.Count > 0 Then
'Loop through the slide shows collection,
'and if the slide show's name is equal to the active presentation's name,
'then get the index number of the active slide.
        For i = 1 To SlideShowWindows.Count
            If SlideShowWindows(i).Presentation.Name = .Name Then
                SlIndex = SlideShowWindows(i).View.Slide.SlideIndex
            End If
        Next i
'If there are no slide shows running the presentation will be in "edit" mode.
    Else
'Loop through the active presentation's windows collection,
'and f the caption of the window is equal to the active one,
'then get the index number of the active slide.
        For i = 1 To ActivePresentation.Windows.Count
            If .Windows(i).Caption = ActiveWindow.Caption Then
                SlIndex = .Windows(i).View.Slide.SlideIndex
            End If
        Next i
    End If
End With
 
MsgBox "The current active slide's index number = " & SlIndex


Instead of referring to a slide by its ID or index number, we could equally well use the slide's name. It is a good habit to add a custom name to each slide you programmatically add, and to store that name in for instance the slide title (which visibility can be set to False), or in the "NotesPage" for later reference.



Public Sub Testing() 
'assumes that there is only one chart per slide.
   Call AddHyperlink(2, 5)
End Sub

Public Sub AddHyperlink( _
   ByVal iSlideWithChart As Integer, _
   ByVal iHyperlinkToSlideNo As Integer)
   
Dim oSlide As PowerPoint.Slide
Dim oChartShape As PowerPoint.Shape
Dim oHyperlinks As PowerPoint.Hyperlinks

   Set oChartShape = ActivePresentation.Slides(iSlideWithChart).Shapes(1)
   
   oChartShape.ActionSettings(ppMouseClick).Action = ppActionHyperlink
   oChartShape.ActionSettings(ppMouseClick).Hyperlink.SubAddress = _
      CStr(ActivePresentation.Slides(iHyperlinkToSlideNo).SlideID) & ",1,Title"
      
End Sub



© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext