PDA

View Full Version : Programmatically add sound effects to Presentation



BarneyB
09-08-2016, 10:43 PM
I would like to be able to hyperlink a substring in a text object and assign a unique sound to it. When I use the following code, I can use the soundeffect.name property to play the build-in laser sound.


Set retrange = .Characters(sl, strlen)
With retrange
With .ActionSettings(ppMouseOver)
.Hyperlink.Address = ""
.Hyperlink.SubAddress = cursld
.Hyperlink.ScreenTip = arrTwoD(i, 4)
.SoundEffect.Name = "laser"
End With
End With


I would like to be able to programatically add sounds to the build-in presentation library, so that in addition to the default sounds that are there (e.g. "laser", "applause", "chime"), the sounds I add (e.g. "my_sound1", "my_sound2", "my_sound3", etc.) could be referenced:


With .ActionSettings(ppMouseOver)
.Hyperlink.Address = ""
.Hyperlink.SubAddress = cursld
.Hyperlink.ScreenTip = arrTwoD(i, 4)
.SoundEffect.Name = "my_sound1"
End With


This can be done in the interactive PP interface when setting up a hyperlink, but I would like to be able to do it programmatically.


Thanks.

John Wilson
09-10-2016, 03:07 AM
You need to use the ImportFromFile Method using the full path to the wav file


.SoundEffect.ImportFromFile ("C:\Users\Optiplex\Desktop\apu.wav")
.SoundEffect.Play

BarneyB
09-10-2016, 08:11 AM
You need to use the ImportFromFile Method using the full path to the wav file


.SoundEffect.ImportFromFile ("C:\Users\Optiplex\Desktop\apu.wav")
.SoundEffect.Play


I tired the code you suggested in the past, and what happens is that I get a run-time error stating that the "Method 'ImportFromFile' of object 'SoundEffect' failed". That is why I wanted to stay with my code as it is currently structured and where I can first add the desired sound, and then reference it in the manner I suggested. Once again, my code works very nicely and simply with existing sounds (e.g. "laser"). If I can programmatically add sounds in advance, that solves my problem. I figure since I can add those sounds interactively in PP (Insert-->Action-->Play Sound-->Other Sound . . .[e.g. c:\my_sound] ), I should be able to write code to do that.

John Wilson
09-10-2016, 08:24 AM
What version of Office do you have?
Are you sure you have the path to the sound file correct?

BarneyB
09-13-2016, 01:13 PM
What version of Office do you have?
Are you sure you have the path to the sound file correct?

I am running Office 2016, and I am sure that the path is correct.

John Wilson
09-13-2016, 09:09 PM
I can only tell you the code is correct and I see Shyam gave you the same advice in another forum. You won't find anyone that knows more than Shyam about PPT vba!

It works perfectly here in 2010 and 2016 and I see the sound appear in the dialogue