Use a variable to select an ActiveX control Checkbox by name (in Word from Excel)?

前端 未结 2 1582
忘掉有多难
忘掉有多难 2021-01-22 17:54

What is wrong with this line of code?

WordDoc.CheckBoxNum.Value = CheckBoxVal

To be clear, I am working with an ActiveX control \"Checkbox\".

相关标签:
2条回答
  • 2021-01-22 18:17

    Unfortunately we cannot refer to an ActiveX control directly by its name using WordDoc.InlineShapes(CheckBoxNum), this only works if we know its index number: WordDoc.InlineShapes(1).

    This means that you have to loop through all the controls, comparing its OLEFormat.Object.Name to the name you are looking for:

    Dim obj As Object
    
    For i = 0 To 6 ' number of rows in table
        'get from excel
        CheckBoxNum = ActiveCell.Offset(i, k + 1).Value
        CheckBoxVal = ActiveCell.Offset(i, k).Value
    
        For Each obj In WordDoc.InlineShapes
            If obj.OLEFormat.Object.Name = CheckBoxNum Then
                obj.OLEFormat.Object.Value = CheckBoxVal
            End If
        Next obj
    Next i
    
    0 讨论(0)
  • 2021-01-22 18:22

    AFAIK, you can use:

    WordDoc.InlineShapes(CheckBoxNum).OLEFormat.Object.Value = CheckBoxVal
    
    0 讨论(0)
提交回复
热议问题