Solved

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

Posted on 2016-09-30
11
142 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 51

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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 500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction In all recent versions of PowerPoint it is possible to trigger animations. This means the animation takes place when a certain shape is clicked. This allows you to run animation “on demand” and outwith the normal sequence of mouse cl…
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 add transitions to their Slideshows and how to set up timing for the transitions.
The viewer will learn how to edit animations within the presentation, incorporate sound, and set everything up with timing.

717 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