Range Oddity

G

Greg Maxey

I wonder why you can add a formfield to a range set to a table cell
range, but you can't add a field to range set to a table cell range.
As the code below illustrates, to add a field to a table cell you first
have to collapse a range assigned to a table cell range:

Sub RangeTest()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Set oRng1 = ActiveDocument.Tables(1).Rows(1).Cells(1).Range
Set oRng2 = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
ActiveDocument.FormFields.Add oRng1, wdFieldFormTextInput
On Error GoTo Err_Handler
ActiveDocument.Fields.Add oRng2, wdFieldQuote, "Test"
Exit Sub
Err_Handler:
oRng2.Collapse wdCollapseStart
Resume
End Sub
 
H

Helmut Weber

Hi Greg,

its endless.

It seems, that for the first range it works.
Not so for the second, unless you reduce the range.

Sub RangeTest()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Set oRng1 = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
Set oRng2 = ActiveDocument.Tables(1).Rows(1).Cells(5).Range
ActiveDocument.FormFields.Add oRng1, wdFieldFormTextInput
oRng2.End = oRng2.End - 1 ' <<< no way without it
ActiveDocument.Fields.Add oRng2, wdFieldQuote, "Test"
End Sub

I wonder why

I wonder, too.

MSFT should, might, hire all MVPs, and Jezebel, too ...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

Consider what happens when you run this:
Sub RangeTest()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim i As Long
For i = 1 To 2
Set oRng1 = ActiveDocument.Tables(1).Rows(1).Cells(2).Range
Set oRng2 = ActiveDocument.Tables(1).Rows(1).Cells(5).Range
ActiveDocument.FormFields.Add oRng1, wdFieldFormTextInput
oRng2.End = oRng2.End - 1 ' <<< no way without it
ActiveDocument.Fields.Add oRng2, wdFieldQuote, "Test"
Next i
End Sub

You get two formfields. Text2 preceeds Text1 in the cell. Apparantly
the FormField (property I guess) causes the range to automatically
collapse to the start of the cell.
 
T

Tony Jollans

The Range is not collapsed. The added Field (or FormField) should replace
the Range but cannot (or will not) do so when the range includes the end of
cell mark. FormFields.Add decides to add before the Range and Fields.Add
just goes belly up. Who knows why they are different? Sometimes I wonder
too!
 

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