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 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.
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.
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.
The object model (VBA) isn't always so forgiving and will simply refuse, often with a not so informative error message.
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"