更改媒体对象(VBA PowerPoint)



我只想使用宏中PowerPoint中的媒体对象的音乐。我的幻灯片中有音乐,但是我不知道如何将其更改为不同的音乐。还是可以用新的属性替换为新属性...?我尝试使用以下代码玩耍,但我不知道其余的...

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

您将需要删除现有形状并用新形状替换,并根据需要复制属性。这本MSDN文章列举了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:Usersdavid.zemensDownloads2540.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

在本文的帮助下,我可以通过创建效果来自动播放音频(请参见上面的Set eff = ...)。

最新更新