0

I am trying to make an automatic textbox checker.

I want it to ensure that there are at least 30 characters in each textbox before allowing the user to move to the next page. How do I access the values of the textboxes?

The specific piece of code

ActivePresentation.Slides(ActivePresentation.SlideShowWindow.View.Slide.SlideIndex).Shapes("TextBox" & CStr(i)).Characters.Text

Public Sub GeneralCheck()

    Dim oSh As Shape
    Dim i As Integer
    Dim TextBoxCounter As Integer
    TextBoxCounter = 0
    On Error Resume Next
    
    For i = 1 To 4
    
        Set oSh = ActivePresentation.Slides(ActivePresentation.SlideShowWindow.View.Slide.SlideIndex).Shapes("TextBox" & CStr(i))
        If Err.Number = 0 Then  ' shape exists
            MsgBox ("I exist")
            MsgBox (Str(Len(ActivePresentation.Slides(2).Shapes("TextBox" & CStr(i)).Characters.Text)))
            TextBoxCounter = TextBoxCounter + 1
        Else
            MsgBox ("I don't exist")
        End If
    
    Next i
    
End Sub

Update:
Changed code still cannot access the text of the TextBox.

Set oSh = ActivePresentation.SlideShowWindow.View.Slide.Shapes("TextBox" & CStr(i))
If Err.Number = 0 Then  ' shape exists
    MsgBox ("I exist")
    ShapeLength = ShapeLength + Len(oSh.TextFrame.TextRange.Text)
    MsgBox (ShapeLength)

The For loop seems to work.

Community
  • 1
  • 1
TheOuz
  • 47
  • 7

2 Answers2

0
  1. You must set the name of the Shape:

  2. Get the Shape Object:
    By name: oShape = oSlide.Shapes("NameOfShape").
    By index: oShape = oSlide.Shapes(index).

  3. To access the text of a shape:

    oShape.TextFrame.TextRange.Text
    
double-beep
  • 5,031
  • 17
  • 33
  • 41
D T
  • 3,522
  • 7
  • 45
  • 89
  • Using this, MsgBox (Str(Len(ActivePresentation.Slides(ActivePresentation.SlideShowWindow.View.Slide.SlideIndex).Shapes("TextBox" & CStr(i)).TextFrame.TextRange.Text))) . However it is still not working – TheOuz Jul 06 '20 at 03:11
  • Is exist name of shape "TextBox1"? – D T Jul 06 '20 at 03:13
  • If you not setting name, you have to use index, ex: slide.Shapes(1) – D T Jul 06 '20 at 03:16
  • There is definitely an object on my slide 2 which has the name "TextBox1 – TheOuz Jul 06 '20 at 03:26
  • When I run my code I get four message boxes one with "I exist" and three with "I dont exist" as expected. I just get no message box telling me the length of the text in textbox1 – TheOuz Jul 06 '20 at 03:28
  • Press: ALT + F10 and check name of your shapes. – D T Jul 06 '20 at 03:29
  • "TextBox1" is the name of the textbox, I would not get the I exist msgBox if not – TheOuz Jul 06 '20 at 03:30
  • Exist TextBox2,3,4 on your slide? – D T Jul 06 '20 at 03:31
  • No only text box 1, I can try put them in as well and try it out – TheOuz Jul 06 '20 at 03:32
  • You accessing TextBox1,2,3,4: by your code: .Shapes("TextBox" & CStr(i)) – D T Jul 06 '20 at 03:33
  • Yeah that's the idea, is that where i am going wrong? – TheOuz Jul 06 '20 at 03:36
  • Yes, your slide not exist TextBox2,3,4 and you accessing them. – D T Jul 06 '20 at 03:37
  • I designed the code to see if "TextBox1" Exists, and if it does then to print the length of the text? How do I change it to work – TheOuz Jul 06 '20 at 03:39
  • You can remove For and change .Shapes ("TextBox" & CStr (i)) to .Shapes ("TextBox1"), and refer my answer to get text. – D T Jul 06 '20 at 04:17
  • @D T that removes the functionality that I am trying to get – TheOuz Jul 06 '20 at 09:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217300/discussion-between-theouz-and-d-t). – TheOuz Jul 06 '20 at 09:45
0

The code i required to get the value of the text box was

oShape.OLEFormat.Object.Value
TheOuz
  • 47
  • 7