Powerpoint Slide Count Variable in VBA

北城余情 提交于 2019-12-12 23:38:27

问题


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:


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


来源:https://stackoverflow.com/questions/45391119/powerpoint-slide-count-variable-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!