尝试在 Word table 单元格中插入字段时出错

Getting an error trying to insert field in a Word table cell

我创建了一个表单域 (textinput),当我给它命名时,我得到: 91 未设置对象变量或 With 块变量

我在这个宏中的许多其他地方都做同样的事情并且工作正常。这是代码:

Private Sub IRPMs()
Dim objCon As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i, j, k As Integer
Dim tmpField As FormField
Dim strOut As String

Set doc = ActiveDocument
i = 0
j = 1
k = 0

Call grabInfo

strFieldName(1) = "Property:  "
strFieldName(2) = "General Liability:  "
strFieldName(3) = "Auto Liability:  "
strFieldName(4) = "Auto Phys Dam:  "
strFieldName(5) = "Inland Marine:  "

'' Remove document protection, if necessary
If doc.ProtectionType <> wdNoProtection Then doc.Unprotect

On Error GoTo IRPMerror

doc.Tables(3).Rows(9).Cells(2).Select
Selection.Delete
doc.Tables(3).Rows(10).Cells(2).Select
Selection.Delete

strSQL = "redacted"

objCon.Open "redacted"

rs.Open strSQL, objCon

If Not rs.EOF Then
    For k = 0 To 4
        If rs(k) <> "" Then i = i + 1
    Next

    doc.Tables(3).Rows(9).Cells(2).Select

    If i = 0 Then
        Selection.EndKey unit:=wdLine

        Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
        tmpField.Name = "irpms1"
    Else
        For k = 0 To 4
            If rs(k) <> "" Then
                Selection.InsertAfter (strFieldName(j))
                Selection.EndKey unit:=wdLine

                Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
                tmpField.Name = "irpms" & j

                If j <= i And j < 5 Then
                    Selection.InsertAfter Chr(11)
                End If

                strOut = rs(k)
                If Left(strOut, 1) = "." Then strOut = "0" & strOut

                doc.FormFields("irpms" & j).Result = strOut
            End If

            j = j + 1
        Next
    End If
Else
    Selection.EndKey unit:=wdLine

    Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
    tmpField.Name = "irpms1"
End If

rs.Close

strSQL = "redacted"

'rs.CursorLocation = adUseClient        ' I've tried with and without setting the cursor
rs.Open strSQL, objCon, adOpenDynamic

MsgBox (rs.RecordCount)
i = rs.RecordCount

If Not rs.EOF Then
    rs.MoveFirst

    doc.Tables(3).Rows(10).Cells(2).Select

    Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
    tmpField.Name = "Ducks"         ' This is merely for testing; throws the error

    If i = 1 Then
        Selection.EndKey unit:=wdLine

        Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
        tmpField.Name = "devi1"
    Else
        For k = 1 To i
            Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
            tmpField.Name = "devi" & k

            'doc.FormFields("devi" & k).Result = rs(0) & " Deviations: " & rs(1)

            If k <> rs.RecordCount Then
                Selection.InsertAfter (Chr(11))
            End If

            rs.MoveNext
        Next
    End If
End If

GoTo IRPMexiter

IRPMerror:
MsgBox ("IRPM" & vbCrLf & Err.Number & vbCrLf & Err.Description)
GoTo IRPMexiter

IRPMexiter:
If Not rs Is Nothing Then Set rs = Nothing
If rs.State Then rs.Close
If Not objCon Is Nothing Then Set objCon = Nothing
If objCon.State Then objCon.Close

If doc.ProtectionType = wdNoProtection Then doc.Protect Type:=wdAllowOnlyFormFields, noreset:=True, Password:=""

If Err Then End

Exit Sub

SQL 和连接参数已被删除,它们似乎没有任何问题。 "grabInfo" 从文档属性中检索数据以传递到 SQL。 "doc" 在其他地方公开声明。

所以,它顺利地完成了第一部分,现在已经有一个多月了。 rs关闭再打开后,不喜欢我用"tmpfield"了

我很感激任何建议。首先 post,如果我可以改进我的问题或者我是否违反了任何规则,请告诉我。

在某些情况下,Word 无法插入某些内容,因为当前选择不支持那种对象。如果在 Word 界面中尝试,作为用户,将出现一条错误消息...或者 Word 将简单地执行 "best guess" 并在最近的有效位置创建对象。

对象模型 (VBA) 并不总是那么宽容并且会简单地拒绝,通常带有信息量不大的错误消息。

在这种情况下,整个 table 单元格被选中,Word 无法在单元格结构内(相对于单元格的文本区域)创建表单域。将选择减少到单个点(闪烁的插入点/光标)将允许创建表单域。 Collapse 方法可以做到这一点,如以下代码示例所示:

doc.Tables(3).Rows(10).Cells(2).Select
Selection.Collapse wdCollapseStart 
Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
tmpField.Name = "Ducks"