以编程方式在PowerPoint中播放形状的声音

时间:2010-07-30 23:05:48

标签: automation audio vsto powerpoint

我正在使用PowerPoint 2007 VSTO加载项,我正在运行一个小问题。加载项使用以下代码将声音添加到当前幻灯片:

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

生成的形状确实有声音,可以通过PowerPoint幻灯片播放。我的问题是,给定一个以这种方式创建的形状的引用,我想以编程方式播放声音,但我找不到这样做的方法。我试过了

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

但是这会失败/崩溃,当我检查soundEffect时,它的类型是ppSoundNone 编辑:

中获得了部分成功
var shape = slide.Shapes.AddMediaObject(fileLocation, 50, 50, 20, 20);
shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation);

这样做可以让我播放声音:

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

然而,有一个主要问题;这仅适用于添加的最后一个形状。出于某种原因,shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation)似乎将SoundEffect属性重置为ppSoundNone以获取先前创建的形状...

如果这不可行,我会感到惊讶,但我似乎无法找到 - 任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:4)

很抱歉VBA,但它很容易移植到C#。这将是有用的:

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