为什么我在从 MS Word 文档中读取单选按钮值时得到 "Run-time error '91': Object variable or With block variable not set"?

Why am I getting "Run-time error '91': Object variable or With block variable not set" when reading radio button values from an MS Word doc?

我编写了一个程序来读取 Microsoft Word 内容控件和 ActiveX 字段数据并将其插入到数据库中。它在我的系统上运行良好。但是,在测试中我得到了“运行-time error '91': Object variable or With block variable not set”错误。我能够确定是单选按钮(ActiveX 对象)是罪魁祸首。我仅为单选按钮创建了以下测试脚本。这是代码:

Sub GetRBData()

Dim shp As InlineShape
Dim sql As String
Dim Fields As String
Dim Values As String

For Each shp In ActiveDocument.InlineShapes
    With shp
        If .Type = wdInlineShapeOLEControlObject And .OLEFormat.Object.Value = True Then
            With .OLEFormat.Object
                Fields = Fields & .GroupName & ", "
                Values = Values & "'" & Right(.Name, Len(.Name) - Len(.GroupName)) & "', "
            End With
        End If
    End With
Next shp

Fields = Left(Fields, Len(Fields) - 2)
Values = Left(Values, Len(Values) - 2)

sql = sql & "INSERT INTO MYTABLE " & "(" & Fields & ")" & " VALUES " & "(" & Values & ")"

MsgBox sql

End Sub

我认为问题可能与 Microsoft 对象库有关。我检查了两个。两者都使用 Microsoft ActiveX Data Objects 6.1 Library。我将不胜感激任何帮助。谢谢。

评论太长...

If .Type = wdInlineShapeOLEControlObject And .OLEFormat.Object.Value = True Then

如果第一个测试失败,则仍然评估第二个测试,如果形状属于没有 .OLEFormat.Object

的类型,这将是一个问题

您应该嵌套测试:

If .Type = wdInlineShapeOLEControlObject Then
    If .OLEFormat.Object.Value = True Then