更改媒体对象 (VBA PowerPoint)

Change Media Object (VBA PowerPoint)

我只想使用宏更改 PowerPoint 中媒体对象的音乐。我在幻灯片中有一段音乐,但我不知道如何将它更改为不同的音乐。或者是否有可能用一个新的但具有相同属性的替换它......? 我尝试使用以下代码,但我不知道其余的...

Slide3.Shapes("bg_music").MediaFormat. 'code that I don't know to change it's music/media

您将需要删除现有形状并将其替换为新形状,并根据需要复制属性。 This MSDN article 枚举了 MediaFormat 属性的一些(全部?)。

Option Explicit

Sub ReplaceMediaFormat()
Dim sld As Slide
Dim newShp As Shape
Dim shp As Shape
Dim mf As MediaFormat
Dim path As String

Set sld = ActivePresentation.Slides(1) '// Modify as needed
Set shp = sld.Shapes("bg_music")
Set mf = shp.MediaFormat

'// Modify the path for your new media file:
path = "C:\Users\david.zemens\Downloads40.mp3"

Set newShp = sld.Shapes.AddMediaObject2(path)
With newShp
    .Top = shp.Top
    .Left = shp.Left
    .Width = shp.Width
    .Height = shp.Height
    ' etc...

End With

' // copy the mediaformat properties as needed

With newShp.MediaFormat
    .StartPoint = mf.StartPoint
    .EndPoint = mf.EndPoint
    .FadeInDuration = mf.FadeInDuration
    .FadeOutDuration = mf.FadeOutDuration
    .Muted = mf.Muted
    .Volume = mf.Volume
    ' etc...
End With

'// remove the original
shp.Delete

Dim eff As Effect
'// Creates an effect in the timeline which triggers this audio to play when the slideshow begins
Set eff = sld.TimeLine.MainSequence.AddEffect(newShp, msoAnimEffectMediaPlay, trigger:=msoAnimTriggerWithPrevious)

With newShp.AnimationSettings.PlaySettings
    .LoopUntilStopped = msoCTrue
    .PauseAnimation = msoFalse
    .PlayOnEntry = msoCTrue
    .RewindMovie = msoCTrue
    .StopAfterSlides = 999
    .HideWhileNotPlaying = msoTrue
End With

this article 的帮助下,我可以通过创建 效果 让音频自动播放(见上文 Set eff = ...)。