Search code examples
vbapowerpoint

Stop playing sound media on click


I want to add two sound shapes to a PowerPoint slide, both triggered on click, using PowerPoint VBA (Microsoft 365 MSO).

The slide timeline will be:

  1. First click starts playing sound
  2. Second click stops the first sound and starts the second.

I am able to place the sound shapes and add the animation objects to trigger the sounds.

I can't find the effect object property which replicates the GUI option to stop playing on click.
GUI option to stop playing on click

The code will add a new slide, create two sound shapes and have them trigger on click, however sound 1 does not stop playing.

Sub TestSoundTrigger()

    Dim slTestSoundSlide As Slide
    Dim shSoundShape1 As Shape
    Dim shSoundShape2 As Shape
    Dim efSoundShape1 As Effect
    Dim efSoundShape2 As Effect

' Create the slide
    Set slTestSoundSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1))

' Add 2 sound shapes
    Set shSoundShape1 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound1.mp3", True, False, 10, 10)
    Set shSoundShape2 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound2.mp3", True, False, 10, 10)
    
' Add the 2 triggers to play the sounds on click in turn
    Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaPlay)
    Set efSoundShape2 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape2, effectId:=msoAnimEffectMediaPlay)

End Sub

I've checked the Effect and the Timeline object properties but I can't find this one.


Solution

  • I found this property: ppSoundStopPrevious ppSoundEffectType

    Alternatively You could set another msoAnimEffectMediaStop - but this would create another animation step

        Sub TestSoundTrigger()
    
        Dim slTestSoundSlide As Slide
        Dim shSoundShape1 As Shape
        Dim shSoundShape2 As Shape
        Dim efSoundShape1 As Effect
        Dim efSoundShape2 As Effect
    
    ' Create the slide
        Set slTestSoundSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1))
    
    ' Add 2 sound shapes
        Set shSoundShape1 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound1.mp3", True, False, 10, 10)
        Set shSoundShape2 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound2.mp3", True, False, 10, 10)
    
    ' Add the 2 triggers to play the sounds on click in turn
        Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaPlay)
        Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaStop)
        Set efSoundShape2 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape2, effectId:=msoAnimEffectMediaPlay)
    
    End Sub