首页 > 解决方案 > 尝试在 Word 表格单元格中插入字段时出错

问题描述

我创建了一个表单域(文本输入),当我去命名它时,我得到: 91 对象变量或未设置块变量

在这个宏中还有很多其他地方我做同样的事情并且效果很好。这是代码:

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”了。

我很感激任何建议。第一篇文章,所以请让我知道我是否可以改进我的问题或者我是否违反了任何规则。

标签: vbams-wordform-fieldsword-table

解决方案


在某些情况下,Word 无法插入某些内容,因为当前选择不支持该类型的对象。如果在 Word 界面中尝试过,作为用户,将出现一条错误消息……或者 Word 将简单地进行“最佳猜测”并在最近的有效位置创建对象。

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

在这种情况下,会选择整个表格单元格,并且 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"       

推荐阅读