Greg
Greg

Reputation: 63

VBA Powerpoint Reference a textbox with variable

I am attempting to write a vba loop that will detect the value of all ActiveX textboxes on the slide. However I am have trouble writing the code for the "variable" in the textbox reference. For example TextBox(i) needs to be referenced in the loop. Where i is an integer I set the value to.

Dim i as Integer

For i = 1 to 4

 If IsNull(Slide1.Shapes.("TextBox" & i).Value) = True 
     Then (Slide1.Shapes.("TextBox" & i).Value) = 0
              Else: ...

Next i

However this script doesn't work and I have been unable to locate a source for how to properly code this variable portion of script. There has been some talk of using Me.Controls however I am not creating a form. Would anyone be willing to share what the error is here in my script?

Upvotes: 0

Views: 3333

Answers (2)

Greg
Greg

Reputation: 63

@Steve Rindsberg you had the correct code. Thank you. Here was the final script to obtain the value, and set the value if blank.

    For i = 1 To 4

            'set oSh to TextBox1, TextBox2, TextBox3... etc.
            Set oSh = ActivePresentation.Slides(1).Shapes("TextBox" & CStr(i))

                'set myVar to value of this TextBox1, TextBox2...
                myVar = oSh.OLEFormat.Object.Value

                    If myVar = "" Then _
                        ActivePresentation.Slides(1).Shapes("Text" & CStr(i)).OLEFormat.Object.Value = 0 _
                    Else: 'do nothing

                    'clear value of myVar
                    myVar = ""

    'start on next integer of i                
    Next i

Upvotes: 0

Steve Rindsberg
Steve Rindsberg

Reputation: 14810

This will put the value of i into TextBox i. Should get you started, I think.

Sub Example()

    Dim oSh As Shape
    Dim i As Integer

    On Error Resume Next

    For i = 1 To 4

        Set oSh = ActivePresentation.Slides(1).Shapes("TextBox" & CStr(i))
        If Err.Number = 0 Then  ' shape exists
            oSh.OLEFormat.Object.Text = CStr(i)
        End If

    Next i

End Sub

Upvotes: 2

Related Questions