Solved

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

Posted on 2016-09-30
11
57 Views
Last Modified: 2016-10-11
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
Comment
Question by:Shannon Bowling
  • 6
  • 4
11 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi,

Could you show the problematic code

Regards
0
 

Author Comment

by:Shannon Bowling
Comment Utility
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
 
LVL 23

Expert Comment

by:JSRWilson
Comment Utility
As Rgonzo says you should post your code so that people can see what is happening
0
 

Author Comment

by:Shannon Bowling
Comment Utility
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
 
LVL 23

Expert Comment

by:JSRWilson
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Shannon Bowling
Comment Utility
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
 

Author Comment

by:Shannon Bowling
Comment Utility
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
 
LVL 23

Expert Comment

by:JSRWilson
Comment Utility
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
 
LVL 23

Accepted Solution

by:
JSRWilson earned 500 total points
Comment Utility
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
 

Author Comment

by:Shannon Bowling
Comment Utility
Yes, this solution is what I am looking for.

Thanks a bunch, really appreciate your help.
0
 

Author Closing Comment

by:Shannon Bowling
Comment Utility
Really appreciate the help, great site.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Setting the Scene PowerPoint is a creative tool in the right hands but it also includes a much underutilised programming dimension. In this beginner level article, we're going to show you some of some key elements of programming PowerPoint using th…
Setting the Scene Animations in PowerPoint are a great tool to convey messages when used carefuly with the content of your slides. There are plenty of animation effects and options, including a Repeat feature for individual animation effects. …
This video teaches viewers how to add transitions to their Slideshows and how to set up timing for the transitions.
The viewer will learn how to edit the master slide. They will also learn how to combine multiple themes into one master slide to use them in their presentation.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now