Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

Range Oddity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 05 Dec 2006 20:53 GMT
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
Helmut Weber - 05 Dec 2006 21:18 GMT
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 ...

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey - 05 Dec 2006 21:28 GMT
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.

> Hi Greg,
>
[quoted text clipped - 26 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
Tony Jollans - 06 Dec 2006 20:26 GMT
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!

Signature

Enjoy,
Tony

> Helmut,
>
[quoted text clipped - 46 lines]
>> Win XP, Office 2003
>> "red.sys" & Chr$(64) & "t-online.de"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.