Duplicate sound media on each slide

gailb
2StarLounger
Posts: 148
Joined: 09 May 2020, 14:00

Duplicate sound media on each slide

Post by gailb »

Some how I've ended up duplicating the media on each slide when running the attach macro.
Two questions,
1) what in this macro is duplicating the media that is already there
2) How can I delete the duplicate media?
The media is already on the slide, this macro should just add the advance time, unmute, and set the volume.

Code: Select all

Sub SoundMediaLength3()
    Dim sld As Slide
    Dim shp As Shape
    Dim myTime As Long
    Dim eff As Effect
    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Type = msoMedia Then
                If shp.MediaType = ppMediaTypeSound Then
                    With sld.SlideShowTransition
                        shp.MediaFormat.Volume = 1    ' scale is 0 to 1
                        shp.MediaFormat.Muted = False  ' True or False
                        .AdvanceOnClick = msoTrue
                        .AdvanceOnTime = msoTrue
                        myTime = Left(shp.MediaFormat.Length, 2)
                        .AdvanceTime = myTime + 1
                        .EntryEffect = ppEffectWipeRight
                    End With
                    'Set audio to play automatically
                    Set eff = sld.TimeLine.MainSequence.AddEffect(shp, msoAnimEffectMediaPlay, , msoAnimTriggerWithPrevious)
                    eff.EffectInformation.PlaySettings.HideWhileNotPlaying = True
                End If
            End If
        Next shp
    Next sld
End Sub

User avatar
HansV
Administrator
Posts: 71216
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Duplicate sound media on each slide

Post by HansV »

I ran the code several times on a short presentation with some audio. The code did NOT duplicate the audio, it just modified the playback settings - as intended.
I suspect there must be something else that duplicates the items.

If the duplicate media items have the same name, you can remove them using the following macro. Please test on a copy of your presentation!

Code: Select all

Sub RemoveDupAudio()
    Dim sld As Slide
    Dim shp As Shape
    Dim i As Long
    Dim dct As Object
    For Each sld In ActivePresentation.Slides
        Set dct = CreateObject("Scripting.Dictionary")
        For i = sld.Shapes.Count To 1 Step -1
            Set shp = sld.Shapes(i)
            If shp.Type = msoMedia Then
                If shp.MediaType = ppMediaTypeSound Then
                    If dct.Exists(shp.Name) Then
                        shp.Delete
                    Else
                        dct.Add Key:=shp.Name, Item:=1
                    End If
                End If
            End If
        Next i
        Set dct = Nothing
    Next sld
End Sub
Regards,
Hans

gailb
2StarLounger
Posts: 148
Joined: 09 May 2020, 14:00

Re: Duplicate sound media on each slide

Post by gailb »

Ok, now I see. It's not duplicating the media, it's actually just duplicating the animation. There was one animation set to start after previous, and not there are multiple animations for the one media.

gailb
2StarLounger
Posts: 148
Joined: 09 May 2020, 14:00

Re: Duplicate sound media on each slide

Post by gailb »

Alright, I think I found something to work. Any idea why the animation is being duplicated?

Code: Select all

Sub RemoveAllAnimations()
    Dim sld As Slide
    Dim x As Long
    Dim Counter As Long
    Dim shp As Shape
    For Each sld In ActivePresentation.Slides
        For x = sld.TimeLine.MainSequence.Count To 1 Step -1
            For Each shp In sld.Shapes
                If shp.Type = msoMedia Then
                    If shp.MediaType = ppMediaTypeSound Then
                        If x <> 1 Then
                            sld.TimeLine.MainSequence.Item(x).Delete
                        End If
                    End If
                End If
            Next shp
            Counter = Counter + 1
        Next x
    Next sld
End Sub

User avatar
HansV
Administrator
Posts: 71216
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Duplicate sound media on each slide

Post by HansV »

The line

Code: Select all

Set eff = sld.TimeLine.MainSequence.AddEffect(...)
adds a new effect each time the SoundMediaLength3 macro is run.

I was just about to suggest code like the one you posted to remove existing effects.
Regards,
Hans

gailb
2StarLounger
Posts: 148
Joined: 09 May 2020, 14:00

Re: Duplicate sound media on each slide

Post by gailb »

Thanks Hans. This is great stuff.
I searched the net before asking, but it seems sometimes when you either say it out loud or somebody gives you a response which drives the search deeper.

User avatar
HansV
Administrator
Posts: 71216
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Duplicate sound media on each slide

Post by HansV »

It's called POP: the Power Of Posting. :grin:
Regards,
Hans