简体   繁体   中英

Muting and Unmuting multiple audio media on one slide in Powerpoint using VBA

I use PowerPoint 2016 on Windows 10. Slide A has a text box and a command button. When the command button is selected, a boolean gbool is set and the presentation advances to Slide B.

ActivePresentation.SlideShowWindow.View.GotoSlide <slideB slideindex> 

On Slide B, there are two audio media objects, AM1 and AM2. What I want to happen is if gbool is True then AM1 should play else AM2 should play. I have used the muted flag to attempt this:

Shape.MediaFormat.Muted = False

The problem I have is that, this sometimes works, other times both audio medias will play. The code looks like this:

Private Sub CommandButton1_Click()
    Dim osl As Slide, oSh As Shape, gbool As Boolean
    gbool = function()

    Set osl = ActivePresentation.Slides(<slideB slideindex>)
    Set oSh = osl.Shapes("AM1")
    oSh.MediaFormat.Muted = gbool
    Set oSh = osl.Shapes("AM2")
    oSh.MediaFormat.Muted = Not gbool

   ActivePresentation.SlideShowWindow.View.GotoSlide <slideB slideindex>
End Sub

Any ideas for the random behaviour and is there a better way to do this?

I think I have an answer - at least this method works all the time. Instead of using the muted property of the MediaFormat object, I now use the PlayOnEntry property of the Play Settings object. So the change looks like this:

Set osl = ActivePresentation.Slides(<slideB slideindex>)
Set oSh = osl.Shapes("AM1")
oSh.AnimationSettings.PlaySettings.PlayOnEntry = gbool
Set oSh = osl.Shapes("AM2")
oSh.AnimationSettings.PlaySettings.PlayOnEntry = Not gbool

If there is a better way, I would appreciate to know it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM