尝试在Word表单元格中插入字段时遇到错误 [英] Getting an error trying to insert field in a Word table cell
问题描述
我创建一个formfield(文本输入),当我命名它时,我得到: 91 未设置对象变量或With块变量
I create a formfield (textinput), and when I go to name it I get: 91 Object variable or With block variable not set
此宏中还有很多其他地方,我可以做同样的事情,而且效果很好.这是代码:
There are dozens of other places in this macro where I do the same exact thing and it works fine. Here is the code:
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"在其他地方公开声明.
SQL and connection parameters have been redacted, they don't seem to have any issues. "grabInfo" retrieves data from the document properties to pass into the SQL. "doc" is declared publicly elsewhere.
因此,它从头到尾都运行良好,并且已经运行了一个多月.在rs关闭并重新打开后,不再喜欢使用"tmpfield"了.
So, it runs through the first part fine, and has for over a month now. After the rs is closed and reopened, it doesn't like me using "tmpfield" anymore.
我感谢任何建议.第一篇文章,所以请让我知道我是否可以改善我的问题或是否违反了任何规则.
I appreciate any advice. First post, so please let me know if I can improve my question or if I broke any rules.
推荐答案
在某些情况下,Word无法插入某些内容,因为当前选择不支持该对象.如果在用户的Word界面中尝试过该操作,则将显示一条错误消息……或者Word将仅做最佳猜测"并在最近的有效位置创建对象.
There are situations when Word is not able to insert something because the current selection does not support that kind of object. If it's tried in the Word interface, as a user, an error message will appear to that effect... Or Word will simply do a "best guess" and create the object in the nearest valid position.
对象模型(VBA)并不总是那么宽容,而是会拒绝,通常会给出不那么丰富的错误消息.
The object model (VBA) isn't always so forgiving and will simply refuse, often with a not so informative error message.
在这种情况下,将选择整个表单元格,并且Word无法在单元格结构内(而不是在单元格的文本区域内)创建表单字段.将选择减少到单个点(闪烁插入点/光标)将允许创建表单字段.如下面的代码示例所示,Collapse
方法可以做到这一点:
In this case, an entire table cell is selected and Word is unable to create a form field within the cell structures (as opposed to within the text area of the cell). Reducing the selection to a single point (blinking insertion point / cursor) will allow creating the form field. The Collapse
method can do this, as the following code sample demonstrates:
doc.Tables(3).Rows(10).Cells(2).Select
Selection.Collapse wdCollapseStart
Set tmpField = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
tmpField.Name = "Ducks"
这篇关于尝试在Word表单元格中插入字段时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!