Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Importing an audio file using VBA in Powerpoint and keeping the animation duration constant

I am trying to attach an audio file to shapes in Powerpoint. I have this part figured out. The problem is that when I attach a file using VBA code, it changes the duration of the animation and messes everything up. Basically I want to create a master Powerpoint with all of the animations and timings, and then I want to be able to import audio files that are attached to various shapes. I have several audio files that I import for each slide, so importing the audio file to begin at the beginning of the slide isn't what I want to do.

Also, does anyone know why went you import an audio file to begin playing at the start of a slide, it waits until half of the duration before it starts playing? I can import them manually and they work fine. But, if I import them using VBA code, the file doesn't start playing until half way through the slide. If I then go in and manually uncheck and then recheck the "Play Across Slides" it works perfectly.  This is driving me crazy.

The answer to the first question is more important than the answer to the second question.

Thanks,
Shannon Bowling
0
Shannon Bowling
Asked:
Shannon Bowling
  • 6
  • 4
1 Solution
 
Rgonzo1971Commented:
Hi,

Could you show the problematic code

Regards
0
 
Shannon BowlingAuthor Commented:
It seems that Powerpoint defaults to 0.5 second when you import an audio file to a shape, regardless of what the duration was initially. Even worse, when you import an audio file to a slide using VBA it changes the duration of all shapes on that slide. The solution is to store all of the durations into an array before you import any audio files, then change the duration of each of the shapes back to the original value from the array after the audio files are imported, a real pain in the back.

Thanks,
0
 
JSRWilsonCommented:
As Rgonzo says you should post your code so that people can see what is happening
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Shannon BowlingAuthor Commented:
SlidesCount = ActivePresentation.Slides.Count ' counts number of slides in active presentation

For k = 1 To SlidesCount 'scrolls through slides
   
    For i = 1 To ActivePresentation.Slides(k).Shapes.Count 'counts number of shapes on each slide k
       
        If ActivePresentation.Slides(k).Shapes(i).AlternativeText <> "" Then 'checks to see if shape has text,  if so then changes adds audio file, Alternative text could say "this shape has an audio file"
           
            With ActivePresentation.Slides(k).Shapes(i).AnimationSettings

            .TextLevelEffect = ppAnimateByAllLevels

            .SoundEffect.ImportFromFile "c:\temp\wav_" & k & "_" & i & ".wav" 'imports audio file

            End With
       
        End If

    Next i

Next k
0
 
JSRWilsonCommented:
The timing is changing because you are using legacy code from 2000. After XP you should use the Timeline.MainSequence object to set animation settings.
0
 
Shannon BowlingAuthor Commented:
Thanks, I am still in the process of figuring out the syntax of the code, I wasn't aware I using legacy code, I will look into it and post any new code that I come up with.

Thanks,
0
 
Shannon BowlingAuthor Commented:
I use code like this to set the durations of the shapes after I attach the .wav files to each shape.

ActivePresentation.Slides(k).Select
            ActiveWindow.Selection.SlideRange(1).TimeLine.MainSequence(i).Timing.Duration = Matrix(k, i)

This values in the matrix were stored from the duration times of each slide (k) and shape(i) prior to attaching an audio file to them.

Are you saying there is a way to attach an audio file to a shape using code other than,

With ActivePresentation.Slides(k).Shapes(i).AnimationSettings

            .TextLevelEffect = ppAnimateByAllLevels

            .SoundEffect.ImportFromFile "c:\temp\wav_" & k & "_" & i & ".wav" 'imports audio file

            End With

I found code to change the animation using the timeline.mainsequence as you suggested, would you use msoAnimEffectMediaPlay instead of msoAnimEffectBounce and if so where do you provide the path to link the wav file?

Sub AddBouncingAnimation()

    Dim sldActive As Slide
    Dim shpSelected As Shape

    Set sldActive = ActiveWindow.Selection.SlideRange(1)
    Set shpSelected = ActiveWindow.Selection.ShapeRange(1)

    ' Add a bouncing animation.
    sldActive.TimeLine.MainSequence.AddEffect _
        Shape:=shpSelected, effectId:=msoAnimEffectBounce

End Sub

Thanks,
0
 
JSRWilsonCommented:
Looks like I am wrong!

The new code should be

Set oshp = ActivePresentation.Slides(1).Shapes(3)
Set osld = oshp.Parent
Set oeff = osld.TimeLine.MainSequence.FindFirstAnimationFor(oshp)
With oeff
.EffectInformation.SoundEffect.ImportFromFile "C:\Users\Optiplex\Desktop\apu.wav"
DoEvents
End With

BUT This still sets the duration to 0.5 secs which is weird. I guess under the hood it is still using legacy code for this.
0
 
JSRWilsonCommented:
Maybe adding a media object that plays with the animation would work for you?

Sub addsound()
Dim oshp As Shape
Dim osld As Slide
Dim oeff As Effect
Dim effT As Long
Dim effD As Single
Dim effPos As Long
Dim omed As Shape
On Error Resume Next
Set oshp = ActivePresentation.Slides(1).Shapes("test")
Set osld = oshp.Parent
Set oeff = osld.TimeLine.MainSequence.FindFirstAnimationFor(oshp)
effT = oeff.EffectType
effD = oeff.Timing.Duration
effPos = oeff.Index
Debug.Print oeff.Index
'change path of course
Set omed = osld.Shapes.AddMediaObject2("C:\Users\Optiplex\Desktop\apu.wav")
omed.Left = -100 ' move off slide
Set oeff = osld.TimeLine.MainSequence.AddEffect(oshp, msoAnimEffectAscend, , msoAnimTriggerOnPageClick, effPos + 1)
oeff.Timing.Duration = effD
oeff.EffectType = effT
Set oeff = osld.TimeLine.MainSequence.AddEffect(omed, msoAnimEffectMediaPlay, , msoAnimTriggerWithPrevious, effPos + 2)
osld.TimeLine.MainSequence(effPos).Delete
Set oeff = osld.TimeLine.InteractiveSequences.Item(1).FindFirstAnimationFor(omed) ' optional remove added trigger
oeff.Delete
End Sub
0
 
Shannon BowlingAuthor Commented:
Yes, this solution is what I am looking for.

Thanks a bunch, really appreciate your help.
0
 
Shannon BowlingAuthor Commented:
Really appreciate the help, great site.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now