?
Solved

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

Posted on 2016-09-30
11
Medium Priority
?
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41825577
Hi,

Could you show the problematic code

Regards
0
 

Author Comment

by:Shannon Bowling
ID: 41832395
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
ID: 41837121
As Rgonzo says you should post your code so that people can see what is happening
0
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 

Author Comment

by:Shannon Bowling
ID: 41837153
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
ID: 41837390
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
 

Author Comment

by:Shannon Bowling
ID: 41837482
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
ID: 41837697
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
ID: 41837881
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 2000 total points
ID: 41838018
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
ID: 41839267
Yes, this solution is what I am looking for.

Thanks a bunch, really appreciate your help.
0
 

Author Closing Comment

by:Shannon Bowling
ID: 41839268
Really appreciate the help, great site.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
This video teaches viewers how to fit pictures into slides, crop and remove backgrounds, and alter photos to look more professional.
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.
Suggested Courses

771 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