Programmatically play shape sound in PowerPoint

I am working on a VSTO PowerPoint 2007 add-in and I am running a small issue. The add-in adds sounds to the current slide using the following code:

var shape = slide.Shapes.AddMediaObject(soundFileLocation, 50, 50, 20, 20); 

The resulting form has a sound and can be played through a PowerPoint slide. My problem is that, given the link to the form that was created this way, I would like to programmatically play the sound, but I cannot find a way to do this. I tried

 var soundEffect = shape.AnimationSettings.SoundEffect; soundEffect.Play(); 

but this does not work / crash, and when I check soundEffect its type is ppsoundNone.
Edit: Gained partial success using

 var shape = slide.Shapes.AddMediaObject(fileLocation, 50, 50, 20, 20); shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation); 

This allows me to play sound with:

 var animationSettings = shape.AnimationSettings; var soundEffect = shape.AnimationSettings.SoundEffect; soundEffect.Play(); 

However, there is one serious problem; this only works to add the last shape. For some reason, shape.AnimationSettings.SoundEffect.ImportFromFile (fileLocation) seems to reset the SoundEffect property for ppSoundNone for previously created forms ...

I would be surprised if this were not possible, but I cannot find how ... any help would be greatly appreciated!

+4
source share
1 answer

Sorry for VBA, but it can be easily ported to C #. Here is what will work:

 Sub addsound1() Dim ap As Presentation : Set ap = ActivePresentation Dim sl As Slide : Set sl = ap.Slides(1) Dim audioShape As Shape soundFileLocation = "C:\droid_scan.wav" Set audioShape = sl.Shapes.AddShape(msoShapeActionButtonSound, 100, 100, 50, 50) With audioShape.ActionSettings(ppMouseClick) .Action = ppActionNone .SoundEffect.ImportFromFile soundFileLocation End With End Sub Sub addsound2() Dim ap As Presentation : Set ap = ActivePresentation Dim sl As Slide : Set sl = ap.Slides(1) Dim audioShape As Shape soundFileLocation = "C:\droid_scan2.wav" Set audioShape = sl.Shapes.AddShape(msoShapeActionButtonSound, 50, 50, 50, 50) With audioShape.ActionSettings(ppMouseClick) .Action = ppActionNone .SoundEffect.ImportFromFile soundFileLocation End With End Sub Sub PlaySound1() Dim sh As Shape Set sh = ActivePresentation.Slides(1).Shapes(1) sh.ActionSettings(ppMouseClick).SoundEffect.Play End Sub Sub PlaySound2() Dim sh As Shape Set sh = ActivePresentation.Slides(1).Shapes(2) sh.ActionSettings(ppMouseClick).SoundEffect.Play End Sub 
+4
source

All Articles