PDA

View Full Version : textbox input to use as slide array



erickd
05-15-2011, 07:22 AM
I thought this would be simple. I have a userform text box to collect a string like "2, 4, 6" and save it to string variable 'sldrng'.

Then I would use that variable in in this line:
Set osldrng = opres.Slides.Range(Array(sldrng))

which returns the error "Item 2, 4 not found in the Slides collection" I am sure that is because the textbox variable is being saved as a single text item "2, 4". How should I be collecting that variable to be able to use it as an array list?

Hope this made sense.

Thanks

Erick

John Wilson
05-15-2011, 08:44 AM
It's not that simple. Your array of slide indexs would need to be integers and it's a string.

To use a string you need to create an array of slide names. This is how I would do it but there may be easier ways.

Sub setrange()
Dim osldrng As SlideRange
Dim strinput As String
Dim rng() As String
Dim namerange() As String
Dim i As Integer
strinput = InputBox("Enter slide numbers seperated by commas")
rng = Split(strinput, ",")
ReDim namerange(0 To 0)
'create names array
For i = 0 To UBound(rng) - 1
If rng(i) < 1 Or rng(i) > ActivePresentation.Slides.Count Then GoTo err
namerange(UBound(namerange)) = ActivePresentation.Slides(Int(rng(i))).Name
ReDim Preserve namerange(0 To UBound(namerange) + 1)
Next i
'strip last unwanted empty value
ReDim Preserve namerange(0 To UBound(namerange) - 1)
Set osldrng = ActivePresentation.Slides.Range(namerange)
Exit Sub
err:
MsgBox "You have chosen numbers badly", vbCritical
End Sub

erickd
05-16-2011, 11:05 AM
Thanks John that worked almost perfectly.

I ended up having to remove the '-1' at the end of the 'For i = 0 to UBound(rng)' line. It was not needed.

I also needed to save the slide.count to an integer variable, to keep it from erroring with false positives.

Other than that it works perfectly. This a great help and great examples for me to learn from.

Erick

John Wilson
05-16-2011, 01:01 PM
You're right No -1 needed I meant to add it to the last UBound line and added it twice!
Top of head coding!