Search code examples
vbafor-loopms-wordtextboxactivex

ActiveX textbox value looping


I'm trying to perform a loop on several activeX textboxes named q1, q2, q3 ... q11. Here's what I tried, but it didn't work:

For i = 1 To 11
    myValue(i) = ActiveDocument.q & i.Value
Next

I also tried "q" & i, (q & i), ("q" & i), etc, but none of it worked either.

It does however work when I'm being specific:

ActiveDocument.q1.Value

What am I missing?


Solution

  • It seems we cannot do it easy way. The following sub works for me.

    Sub tst()
    Dim myValue(1 To 3)
    Dim shp As InlineShape
    Dim i As Long 'counter
    On Error Resume Next    
    
    For i = 1 To 3
        For Each shp In ActiveDocument.InlineShapes
            If Not shp.OLEFormat Is Nothing And _
                shp.OLEFormat.ClassType = "Forms.TextBox.1" And _
                shp.OLEFormat.Object.Name = "q" & i Then
            myValue(i) = shp.OLEFormat.Object.Text
            End If
        Next
    Next
    End Sub
    

    If it does not work, try opening the Immediate window (Ctrl+G), step into the sub (F8), move to the If statement and try checking every If clause separately in the Immediate window like this:

    ?Not shp.OLEFormat Is Nothing
    

    You should get True for all the three clauses. If this part is ok then look at what happens with the line where you put values to your array.