Log in

View Full Version : [SOLVED:] VBA Create Last Slide



Viewbasic
12-12-2008, 05:29 PM
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

Demosthine
12-13-2008, 06:11 PM
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.



Dim strResponse As String
strResponse = InputBox("How many slides do you want to insert?", _
"Slides", "10")


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.



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


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.



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



The final code appears as follows:


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


Good Luck
Scott

Viewbasic
12-14-2008, 03:22 PM
Wow, I appreciate the help. This has helped me greatly.