Re: Inserting a wdFieldFormTextInput into a word table cell by Greg
Greg
Tue Dec 05 07:43:01 CST 2006
Helmut,
You might find this interesting:
Sub RangeEffects()
Dim oRng1 As Range
Dim oRng2 As Range
Set oRng1 = ActiveDocument.Tables(1).Rows(1).Cells(1).Range
Set oRng2 = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
oRng1.Collapse wdCollapseStart
With ActiveDocument
'Next line fails if oRng1 is not collapsed
.Fields.Add oRng1, wdFieldEmpty, "MERGEFIELD TESTMERGER"
'Next line runs without error with range expanded
.FormFields.Add oRng2, wdFieldFormTextInput
.FormFields(1).Result = "XXX"
End With
'Here is something else odd
Set oRng1 = ActiveDocument.Tables(1).Rows(1).Cells(3).Range
With ActiveDocument
'.Fields.Add oRng1, does not generate an intellisense dropdown of
available fields
'.FormFields.Add oRng2, does generate an intellisense dropdown.
'all field types are listed. So you can
construct
'the following line with intellisense:
On Error Resume Next
.FormFields.Add oRng1, wdFieldEmpty
'Which understandably fails with a RTE 9, subscript out of range
If Err.Number = 9 Then
MsgBox "RTE " & Err.Number & " " & Err.Description
Err.Clear
End If
'But this line that runs as expected can't be constructed using
intellisense!!
.Fields.Add oRng1, wdFieldEmpty, "MergeField TestMerger"
End With
End Sub
Helmut Weber wrote:
> Hi,
>
> >I've tried multiple ways of adding the merge field
>
> so have I.
> The special difficulty was, that there is no trace
> of a mergefield at all in your code.
>
> I can't recall, all that went wrong.
> Whether its my fault or whether there are unrecovered bugs.
>
> Finally, I arrived at this,
> but I wonder what it could be good for.
>
> Sub Macro9()
> Dim rngTmp As Range
> Set rngTmp = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
> ActiveDocument.FormFields.Add _
> Range:=rngTmp, Type:=wdFieldFormTextInput
> With ActiveDocument.FormFields
> .Item(.Count).Result = "xxx"
> End With
> End Sub
>
> Creating a new formfield-object and setting its result failed.
>
>
> --
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"