Consulting

Results 1 to 3 of 3

Thread: VBA Create Last Slide

  1. #1

    VBA Create Last Slide

    Hello I'm currently working on a project from a book, but I'm having trouble with a particular case.

    In this case,it requires me to create both a title slide and a end slide. And an input box which appears and asks the user how many slides he or she wan'ts and the inputed number translates into the number of slides.

    My problem is I do not know how to use how a inputbox to determine the number of slides. Also, I don't know how to put the last slide.

    Any help would be appreciated .

    JP

  2. #2

    InputBox

    Good Evening.

    The InputBox is a very simple concept to implement, but with that simplicity comes a lack of control. You specify the question to ask, the title, a default value, and maybe the position of the screen you want the InputBox to appear at.

    To start, declare a Variable that will store the value the User inputs. This should be a string, since you can not restrict what the User inputs.

    Next, you'll set that variable equal to the return value of the InputBox Function. As I said, there are several arguments you can set. The first is the question you ask the User. The second is the Title and the third is the Default Value.

    [VBA]
    Dim strResponse As String

    strResponse = InputBox("How many slides do you want to insert?", _
    "Slides", "10")
    [/VBA]

    Now that you have executed that command, you have the User's response stored as strResponse. But since the User can input anything they want, you must check to ensure it is a numeric value.

    [VBA]
    If IsNumeric(strResponse) Then
    ' Process your Insert Slides code here.
    ...
    Else
    MsgBox "You must enter only numeric values into the InputBox!", _
    vbOKOnly, "Numeric Values Required"
    End If
    [/VBA]

    That should solve your InputBox issue.


    Next, inside the If statement, you'll want to create the new slides.

    To start, define a new Variable that will enable you to easily access the newly created slide and then create the new Slide.

    Using the Slides Collection of the ActivePresentation, you will execute the Add Method. There are two required arguments to the Add Method. The first is the Index, which correlates to the location of the new Slide. Value 1 is the very first cell. The second argument is a Constant Literal for which Layout the new Slide will use.

    You'll want to utilize a For ... Next Loop to insert all of the forms. For demonstration purposes, this adds the User specified number of slides at the end of the presentation, which also solves your last issue.

    [VBA]
    Dim intIndex As Integer
    Dim sldNew As Slide

    For intIndex = 1 to strResponse
    Set sldNew = ActivePresentation.Slides.Add( _
    ActivePresentation.Slides.Count + 1, ppLayoutTextAndObject)
    Next intIndex
    [/VBA]


    The final code appears as follows:
    [VBA]

    Public Sub Test()
    Dim prsActive As Presentation
    Set prsActive = ActivePresentation
    Dim strResponse As String
    strResponse = InputBox("How many slides do you want to insert?", "Slides", "10")
    If IsNumeric(strResponse) Then
    ' Create the slides.
    Dim intIndex As Integer
    Dim sldNew As Slide
    For intIndex = 1 To strResponse
    Set sldNew = ActivePresentation.Slides.Add( _
    ActivePresentation.Slides.Count + 1, ppLayoutTextAndObject)
    Next intIndex

    Set sldNew = ActivePresentation.Slides.Add( _
    ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly)
    Else
    MsgBox "You must enter a numeric value only!", vbOKOnly, "Numeric Value Required"
    End If
    End Sub
    [/VBA]

    Good Luck
    Scott
    You don't understand anything until you learn it more than one way. ~Marvin Minsky

    I never teach my pupils; I only attempt to provide the conditions in which they can learn. - Albert Einstein

  3. #3
    Wow, I appreciate the help. This has helped me greatly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •