PDA

View Full Version : Loop through Userform Textboxes to enter text on multiple PPT slides (Edit Mode)



Operon
01-16-2011, 01:30 PM
I am trying to build a Userform that will operate in PPT edit mode to enter text into multiple slides of a presentation.

1) There are 25 identical slides each with "Subject", "Question" and "Answer" textboxes. [Starts on Slide 4.]

2) The Userform has 5 pages each page with 11 Textboxes. The first textbox of each page is the "Subject" textbox, followed by 5 pairs of "Question" and "Answer" textboxes.

When the user clicks the form's Submit CommandButton the Userform's textboxes should populate the relevant textboxes on the presentation's slides.

The following Submit button's code of Userform is not working correctly.

Private Sub CommandButton1_Click()

Dim pPage As Integer
Dim cSlide As Integer
Dim cCnt As Long
Dim cTbox As Long
Dim tTbox As Long

cTbox = 1
tTbox = 2
cCnt = 0
pPage = 1

For cSlide = 4 To 28 Step 1

With ActivePresentation.Slides(cSlide)
.Shapes("Title1").TextFrame.TextRange.Text = Me.MultiPage1.Pages(pPage).TextBox(cTbox).Text
.Shapes("Ques1").TextFrame.TextRange.Text = Me.MultiPage1.Pages(pPage).TextBox(tTbox).Text
tTbox = tTbox + 1
.Shapes("Ans1").TextFrame.TextRange.Text = Me.MultiPage1.Pages(pPage).TextBox(tTbox).Text
End With
cCnt = cCnt + 1
If cCont > 5 Then
pPage = pPage + 1
cTbox = tTbox + 1
tTbox = tTbox + 2
cCnt = 0
Else: tTbox = tTbox + 1
End If

Next cSlide

End Sub
Any helpful suggestions gratefully appreciated.

Also, I am unsure of how best to have user load(show) userform from presentation's edit mode. Perhaps an ActiveX command button?

Many thanks in advance.

Cheers,

John

John Wilson
01-16-2011, 02:59 PM
I haven't done much with multipage but I think you will find the TextBoxes are children of the form not as in your code, children of the multipage.

Command buttons do not work in edit mode you will need to build a menu item or ribbon entry to call the form (or use the macros menu of course)

Operon
01-16-2011, 09:28 PM
Hi John,

Many thanks for your swift reply. I took your advice and tinkered with the code which is presented below. Alas, it still doesn't work.
Private Sub CommandButton1_Click()

Dim pPage As Integer
Dim cSlide As Integer
Dim cCnt As Long
Dim cTbox As Long
Dim tTbox As Long
Dim cCtl As Control

cTbox = 1
tTbox = 2
cCnt = 0
pPage = 1

For cSlide = 4 To 28 Step 1

With ActivePresentation.Slides(cSlide)


For Each cCtl In Me.MultiPage1.Pages(pPage).Controls

If TypeName(cCtl) = "TextBox" And cCtl.Name = "TextBox" & cTbox Then

.Shapes("Title1").TextFrame.TextRange.Text = cCtl.Text

End If

Next cCont

For Each cCtl In Me.MultiPage1.Pages(pPage).Controls

If TypeName(cCtl) = "TextBox" And cCtl.Name = "TextBox" & tTbox Then

.Shapes("Ques1").TextFrame.TextRange.Text = cCtl.Text
tTbox = tTbox + 1

End If
Next cCont

For Each cCtl In Me.MultiPage1.Pages(pPage).Controls

If TypeName(cCtl) = "TextBox" And cCtl.Name = "TextBox" & tTbox Then

.Shapes("Ans1").TextFrame.TextRange.Text = cCtl.Text

End If

Next cCont

End With
cCnt = cCnt + 1
If cCnt > 5 Then
pPage = pPage + 1
cTbox = tTbox + 1
tTbox = tTbox + 2
cCnt = 0
Else: tTbox = tTbox + 1
End If

Next cSlide

End Sub Any thoughts?

Cheers,

John

John Wilson
01-17-2011, 07:23 AM
You are still treating the textboxes as children of the multipage - I don't think they are

Try this approach

Rename to textboxes so on Page one they are
Title1
Question1 Answer1
Question2 Answer2
Question3 Answer 3
Question4 Answer4
Question 5 Answer 5

Then on Page 2
Title2
Question 6 Answer 6 etc

Then code something like this should work

Private Sub CommandButton1_Click()
Dim lng_Title As Long
Dim lng_QandA As Long
Dim lng_slide As Long
Dim lng_adjust As Long
lng_slide = 4
For lng_Title = 1 To 5
For lng_QandA = 1 To 5
With ActivePresentation.Slides(lng_slide + 3)
.Shapes("Title 1").TextFrame.TextRange = Me.Controls("Title" & CStr(lng_Title)).Text
.Shapes("Ques1").TextFrame.TextRange = Me.Controls("Question" & CStr(lng_QandA + lng_adjust)).Text
.Shapes("Ans1").TextFrame.TextRange = Me.Controls("Answer" & CStr(lng_QandA + lng_adjust)).Text
lng_slide = lng_slide + 1
End With
Next
lng adjust = lng_adjust + 5
Next
End Sub
Note NOT tested

Cosmo
01-17-2011, 09:55 AM
As John mentioned, the controls are not children of the Multipage.

Also, your For/Next variables are not named the same:

For Each cCtl ...

Next cCont ' This should be cCtl

Operon
01-19-2011, 02:39 AM
Hi John,

I have built several Userforms in Excel where I used the Loop through Multipage paradigm to, among other things validate textboxes. Eg. Loop Through Specified Controls on all Pages of a MultiPage Control. (http://www.ozgrid.com/VBA/control-loop.htm) Though I do realize that the TextBox control is a child of the Userform, I just figured that restricting the loop to the multipage would be more efficient than marching through the entire Userform. That said I was able to get your kindly provided code to work. The working code follows:
Private Sub CommandButton1_Click()
Dim lng_Title As Long
Dim lng_QandA As Long
Dim lng_slide As Long
Dim lng_adjust As Long
lng_slide = 4
For lng_Title = 1 To 5
For lng_QandA = 1 To 5
With ActivePresentation.Slides(lng_slide)
.Shapes("Title 1").TextFrame.TextRange = Me.Controls("Title" & CStr(lng_Title)).Text
.Shapes("Ques1").TextFrame.TextRange = Me.Controls("Question" & CStr(lng_QandA + lng_adjust)).Text
.Shapes("Ans1").TextFrame.TextRange = Me.Controls("Answer" & CStr(lng_QandA + lng_adjust)).Text
lng_slide = lng_slide + 1
End With
Next
lng_adjust = lng_adjust + 5
Next
End Sub

Cosmo,

I did notice that typo about 30 seconds after I submitted the post. That particular corrected code alas, did not operate.

John: Many thanks for your assistance. A donation however modest will be forthcoming.

Cheers,

John