通过 VBA 的 Word 文档上的复选框 - >运行时错误“5941”

checkboxes on word document via vba -> run time error '5941'

提问人:tehomas 提问时间:10/25/2023 最后编辑:braXtehomas 更新时间:10/25/2023 访问量:34

问:

我有一个 excel 图表,根据不同列中的答案(全范围、使用范围、单点比较),应该在属于上述选项之一的相应复选框处做一个叉。基本上,这是一项相当简单的任务,但我总是收到运行时错误 5941:集合的请求成员不存在。而且我不知道为什么,相应的 word 文档中的复选框每个都与 vba 代码中的名称完全相同。我在这里做错了吗? 错误发生在 doctest 行中。FormFields(“Checkox1”)...

Dim appWd As New Word.Application
Dim wbXL As New Excel.Application
Dim userInput As String
    userInput = InputBox("Enter the number of WX-Form:", "WX-Form Number")

'for the loop

Dim newInput As String
Dim newRange As String
Dim searchcolumn As Range
Dim Cal_Bereich As String
Dim Lieferanten_Cal_Zertifikate As String

AmountWX = WorksheetFunction.CountIf(Range("A3:A2000"), userInput)

'every row is a formular

For b = 1 To AmountWX
    newInput = userInput & " Nr." & b
    
    Sheets("Sheet1").Activate
    'first row with userinput should be found
    Set searchcolumn = Range("A:A")
    For Each cell In searchcolumn
        If cell.Value = userInput Then
            ' when input was found the loop should finish
            firstrow = cell.Row
            Exit For
        End If
    Next Zelle
    newRange = "B" & firstrow + b - 1
    Range(newRange) = newInput
      
    Set appWd = New Word.Application
    appWd.Visible = True
    Set doctest = appWd.Documents.Add("C:\Users\formular.docx")
    doctest.Activate
    Sheets("Sheet1").Activate
    Application.ScreenUpdating = False

'some bookmark assignements

Cal_Bereich = Application.WorksheetFunction.VLookup(Range(newRange), Range("B3:N2000"), 13, False)
    Select Case Cal_Bereich
        Case "Full range"
            doctest.FormFields(1).CheckBox.Value = True
        Case "Use range"
            doctest.FormFields("Checkbox2").CheckBox.Value = True
        Case "Single point comparison"
            doctest.FormFields("Checkbox3").CheckBox.Value = True
    End Select
    Set Anmeldeformular = Nothing
    Set appWord = Nothing
        
Next b

End Sub
Excel VBA excel 公式 ms-word

评论

0赞 FunThomas 10/25/2023
当您要设置复选框并选中 word 文档的 -Collection 时,为什么不简单地设置断点。为此,请使用“局部变量”或“监视”窗口。FormFields
0赞 jonsson 10/25/2023
显而易见的问题是:您在 Word 文档中插入的控件是否肯定是旧版 FormField 复选框,而不是 ContentControl 复选框或 ActiveX 复选框?它们都需要不同的代码来设置其值。

答: 暂无答案