vba - 尝试在 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”了。
我很感激任何建议。第一篇文章,所以请让我知道我是否可以改进我的问题或者我是否违反了任何规则。
解决方案
在某些情况下,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"
推荐阅读
- karate - 场景示例中的 jsonPath
- email - 为什么 SMTP-Server 在响应 DATA 中给出“500 5.5.2 Error: bad syntax”?
- python - 查找矩阵大对角线以下的所有元素
- ios - 当用户单击 ios 13.0 中的登录按钮时,Facebook 登录总是被取消,但它在 ios 12.0 中完全可以正常工作,或者在 swift 中更小
- javascript - 如何在列表中的按钮上使用间隔进行触发单击
- javascript - 同步执行 NodeJS CLI 命令 - 无法在 child_process exec 的关闭事件上放置/创建新文件(嵌套执行命令)
- angularjs - 使用 AngularJS 打印 div 内容
- pine-script - 在 Pine Tradeview 代码中连接条的线
- twitter - "home?status=" Twitter 分享 URL 不再起作用了?
- python - 使用 Pandas 删除具有特定关键字的空值:NaN 值或字符串到浮点转换问题?