Inserting a wdFieldFormTextInput into a word table cell

I

i.d.gibbons

I'm having issues inserting a wdFieldFormTextInput into a word table
cell. I create the table with a macro which works perfectly. I then
need to insert a wdFieldFormTextInput into the first rows 2nd cell.

I can set the text easily enough using ..............

Code:
MyRange.Table(1).Rows(1).Cells(2).Range.Text = " Testy Text "


I've tried multiple ways of adding the merge field all of which have
failed. I'm grateful for any advice! Here is an example of what I've
tied to do to accomplish what i'm after!

Code:
ActiveDocument.Fields.Add
Range:=MyRange.Tables(1).Rows(1).Cells(2).Range,
Type:=wdFieldFormTextInput, Text:="MERGEFIELD  TESTMERGER",
PreserveFormatting:=True

I get the generic "Run-time error '4605': This command is not
available" error message when trying to run the above.
 
H

Helmut Weber

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"
 
G

Greg Maxey

First you can not add a field to the range of a cell. You have to
collapse the range so that it is merely "in" the cell. Secondly, are
you sure you want a "FormField" and not merely a Merge field?

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
oRng.Collapse wdCollapseStart
ActiveDocument.Fields.Add oRng, Type:=wdFieldEmpty, Text:="Mergefield
TESTMERGER"
End Sub
 
G

Greg Maxey

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top