Search code examples
vbams-wordradio-buttonactivex

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?


I wrote a program that reads Microsoft Word Content Control and ActiveX field data and inserts it into a database. It runs great on my system. However, in testing I got "Run-time error '91': Object variable or With block variable not set" error. I was able to determine that it was radio buttons (ActiveX objects) that are the culprit. I created the following test script for radio buttons only. Here is the code:

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

I thought maybe the issue is with Microsoft Object Libraries. I checked both. Both are using Microsoft ActiveX Data Objects 6.1 Library. I would greatly appreciate any help. Thank you.


Solution

  • Too long for a comment...

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

    If the first test fails then the second test is still evaluated, which would be a problem if the shape is of a type which has no .OLEFormat.Object

    You should nest the tests:

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