Jack Armstrong
Jack Armstrong

Reputation: 1249

Powerpoint Slide Count Variable in VBA

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

Upvotes: 3

Views: 11324

Answers (1)

James
James

Reputation: 1972

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

Upvotes: 2

Related Questions