3

I am creating a Powerpoint in vba. My plan is to set up the code such that each subroutine creates a certain slides. However I need to pass through the slide number to each subroutine in order for it to create the proper slide in the right position. I am having trouble defining what the .Slides.Count is in excel vba. I understand it is technically a Long, but somehow I was able to pass it through as an Integer in another patch of code that is no longer working.

My questions are:

  1. The .Slides.Count function. Is that technically a Long or an Integer. If it is a Long, why is it defined that way? Is it because integers have a cap and longs do not?

  2. How should I pass the .Slides.Count variable through into my subroutine that creates a new slide? As an Integer or Long?

I have some example code:

Sub CreatePres()

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppTextbox As PowerPoint.Shape

Set ppApp = New PowerPoint.Application
ppApp.Visible = True
ppApp.Activate

Set ppPres = ppApp.Presentations.Add
slidesCount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(slidesCount + 1, ppLayoutTitle)
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
slidesCount = slidesCount + 1

Call slide2(slidesCount)

End Sub
Sub slide2(i As Integer)
Set ppSlide = ppPres.Slides.Add(i + 1, ppLayoutTitle)
ppSlide.Select
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date

End Sub
1
  • Slides.Count is a Long, but you haven't dimmed slidesCount in your code so VBA is probably treating it as a Variant. ALWAYS include Option Explicit as the first line in any VB/VBA module; that'll force you to properly DIM your variables before using them. Why Long? As I understand it, it's because Longs are more efficient than Integers. Also, note that you don't need ppSlide.Select in Sub Slide2. Commented Jul 30, 2017 at 17:01

1 Answer 1

2

http://vba.relief.jp/powerpoint-vba-get-active-slide-number/

The way to set a variable of the current slide is
foo = ActiveWindow.Selection.SlideRange.SlideIndex

So then call the function with Call slide2(slidesCount)

Try the following

Sub CreatePres()
    Dim ppApp As PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSlide As PowerPoint.Slide
    Dim ppTextbox As PowerPoint.Shape

    Set ppApp = New PowerPoint.Application
    ppApp.Visible = True
    ppApp.Activate

    Set ppPres = ppApp.Presentations.Add
    slidesCount = ppPres.Slides.Count
    Set ppSlide = ppPres.Slides.Add(slidesCount + 1, ppLayoutTitle)
    ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
    ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
    slidesCount = ActiveWindow.Selection.SlideRange.SlideIndex
    Call slide2(slidesCount)
End Sub

Sub slide2(i As Integer)
    Set ppSlide = ppPres.Slides.Add(i + 1, ppLayoutTitle)
    ppSlide.Select
    ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
    ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.