2017-08-30 117 views
0

我只想使用宏更改PowerPoint中的媒體對象的音樂。我在幻燈片中播放了音樂,但我無法弄清楚如何將它改變爲不同的音樂。或者是否有可能用新的替換它,但具有相同的屬性......? 我試圖玩弄下面的代碼,但我不知道休息...更改媒體對象(VBA PowerPoint)

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

回答

0

你將需要一個新的刪除現有的形狀和替換它,複製性需要。 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\Downloads\2540.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 = ...)自動播放。

+0

好的,謝謝。但我只需要再做一件事,如何將「背景播放」更改爲真(使其在背景中,每張幻燈片中播放)? –

+0

@DanielClímaco如果你使用VBE中的Locals窗口來查看對象屬性,它看起來像你可以通過'newShp.AnimationSettings.PlaySettings'來實現。 –

+0

我無法找到屬性「玩/通過幻燈片之間」,這就是我想要的(只是忘了名字) –