尝试在Word VBA中命名自定义formfield时遇到错误

时间:2019-01-02 16:50:47

标签: vba ms-word form-fields

我创建一个formfield(文本输入),当我命名它时,我得到: 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”。

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

1 个答案:

答案 0 :(得分:0)

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

对象模型(VBA)并不总是那么宽容,而是会拒绝,通常会带有不太丰富的错误消息。

在这种情况下,将选择整个表单元格,并且Word无法在单元格结构内(而不是在单元格的文本区域内)创建表单字段。将选择减少到单个点(闪烁插入点/光标)将允许创建表单字段。如以下代码示例所示,WinGet, WinPID, PID, ahk_id %hwnd% oldHwnd := WinExist("ahk_pid " WinPID) Send !{Space} newHwnd := oldHwnd while (newHwnd == oldHwnd) newHwnd := WinExist("ahk_pid " WinPID) 方法可以做到这一点:

Collapse