Compilation error.: Not valid outside the procedure.Which error should I correct?

Asked 2 years ago, Updated 2 years ago, 109 views

With ActivePresentation.Slides(1).SlideShowTransition
    .Speed=ppTransitionSpeedFast
    .EntryEffect=ppEffectStripsDownLeft
    .SoundEffect.ImportFromFile "c:\sndsys\bass.wav"
    .AdvanceOnTime=True
    .AdvanceTime = 5
End With

ActivePresentation.SlideShowSettings.AdvanceMode=_
ppSlideShowUseSlideTimings
    
Next sld

With ActivePresentation.Slides(2).SlideShowTransition
    .Speed=ppTransitionSpeedFast
    .EntryEffect=ppEffectStripsDownLeft
    .SoundEffect.ImportFromFile "c:\sndsys\bass.wav"
    .AdvanceOnTime=True
    .AdvanceTime = 10
End With

ActivePresentation.SlideShowSettings.AdvanceMode=_
ppSlideShowUseSlideTimings

Next sld

With ActivePresentation.Slides(3).SlideShowTransition
    .Speed=ppTransitionSpeedFast
    .EntryEffect=ppEffectStripsDownLeft
    .SoundEffect.ImportFromFile "c:\sndsys\bass.wav"
    .AdvanceOnTime=True
    .AdvanceTime=92
End With

ActivePresentation.SlideShowSettings.AdvanceMode=_
ppSlideShowUseSlideTimings
    
End Sub
Sub Screen Switching Time ( )

End Sub

vba powerpoint

2022-09-30 19:27

1 Answers

In program-specific wording, a function with no return value (=a lump of processing) is sometimes referred to as a procedure.
Also confusingly, VBA describes the procedure as a lump of processing that starts with Sub and ends with End Sub like Sub Screen Switching Time().
Therefore, it is common to write in a procedure to perform a particular action.
(There are exceptions such as declaration by Option Explicit, but I will omit them because they are complicated.)

As you can see in the comments, the reason for this compilation error may be that the following code does not have Sub but has End Sub and Sub Screen Switching Time () does not contain anything.

End Sub'There is no corresponding Sub, but End Sub is present
Sub Screen Switching Time ( )
  There's nothing inside.
End Sub

You may have misappropriated the sample code from the previous question, but sld is a variable declared as a Slide type.
The For Each statement replaces each page of the slide with the sld and transitions to the next page with the For Each xxx to Next xxx syntax as shown below is valid.

Dimsld Aslide

 For Each sld In ActivePresentation.Slides
    ' some sort of treatment
  Next sld

However, if For Each is missing and Next sld is present, the error is similar to "End Sub is present without Sub.

Based on the above two points, I was able to resolve the compilation error by rewriting the code below.

Sub Screen Switching Time ( )
    With ActivePresentation.Slides(1).SlideShowTransition
        .Speed=ppTransitionSpeedFast
        .EntryEffect=ppEffectStripsDownLeft
        .SoundEffect.ImportFromFile "c:\sndsys\bass.wav" 
        .AdvanceOnTime=True
        .AdvanceTime = 5
    End With

    ActivePresentation.SlideShowSettings.AdvanceMode=_
    ppSlideShowUseSlideTimings

    With ActivePresentation.Slides(2).SlideShowTransition
        .Speed=ppTransitionSpeedFast
        .EntryEffect=ppEffectStripsDownLeft
        .SoundEffect.ImportFromFile "c:\sndsys\bass.wav" 
        .AdvanceOnTime=True
        .AdvanceTime = 10
    End With

    ActivePresentation.SlideShowSettings.AdvanceMode=_
    ppSlideShowUseSlideTimings

    With ActivePresentation.Slides(3).SlideShowTransition
        .Speed=ppTransitionSpeedFast
        .EntryEffect=ppEffectStripsDownLeft
        .SoundEffect.ImportFromFile "c:\sndsys\bass.wav" 
        .AdvanceOnTime=True
        .AdvanceTime=92
    End With

    ActivePresentation.SlideShowSettings.AdvanceMode=_
    ppSlideShowUseSlideTimings

    'What's more
    Switch between screens with Call arguments (4,6)
    Switch the screen with the Call argument (5,11)
    Switch to screen with Call argument (6,93)
End Sub

Switch Screen with Sub Arguments (PageNo As Integer, AdvanceTime As Integer)
    With ActivePresentation.Slides (PageNo).SlideShowTransition
        .Speed=ppTransitionSpeedFast
        .EntryEffect=ppEffectStripsDownLeft
        .SoundEffect.ImportFromFile "c:\sndsys\bass.wav" 
        .AdvanceOnTime=True
        .AdvanceTime =AdvanceTime
    End With
End Sub

In addition, the Sub function can be called from any location with arguments as a bonus.
In the sample code, 'The free gift specifies when to switch between pages 4 and 6 in 6, 11 and 93 seconds.


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.