PDA

View Full Version : Solved: Field/range



Sirfanta
10-04-2007, 04:49 AM
I’m trying to not use selection, is there a way to add field by using range?

I want to put some field inside cells in a table.

I know that this doesn’t work (its some sort of explanation).


Sub Test()
Dim jtab As Table
Dim jRow As Row
Dim felt1 As Word.field
Dim felt2 As Word.field

Set jtab = ActiveDocument.Tables(8)
Set jRow = jtab.Range.Rows(3)

Set felt1 = Selection.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="STYLEREF Test1 \*MERGEFORMAT", _
PreserveFormatting:=True)

Set felt2 = Selection.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="STYLEREF Test2 \*MERGEFORMAT", _
PreserveFormatting:=True)


jRow.Cells(3).Range.Text = "This" & felt1 & "fun" & felt2 & "!!!"


End Sub


: pray2:

fumei
10-04-2007, 07:11 AM
Sub Test()
Dim jRow As Row
Dim r As Range
Dim felt1 As Word.Field
Dim felt2 As Word.Field

Set jRow = ActiveDocument.Tables(1).Rows(3)
Set r = jRow.Cells(1).Range
r.Collapse Direction:=wdCollapseStart
Set felt1 = ActiveDocument.Fields.Add(Range:=r, _
Type:=wdFieldUserName)
Set r = jRow.Cells(2).Range
r.Collapse Direction:=wdCollapseStart
Set felt2 = ActiveDocument.Fields.Add(Range:=r, _
Type:=wdFieldDate)
jRow.Cells(3).Range.Text = "This " & felt1.Result & _
" fun " & felt2.Result & "!!!"
End SubComments:

1. Watch out for your text string in cell(3). You need some spaces...

2. While Help states that when inserting a field with a range, if the range is not collapsed the fields replaces the range.....collapse the range! At least in Word 2002, it fails if it is not collapsed.

3. You don't really need the table object, unless you have other uses for having it. As it is, you declare it, Set it...but never use it.

4. You may want to use .Result to get the field data.

Sirfanta
10-05-2007, 04:39 AM
Thanks for the tips fumei! :clap:
As always you deliver! :bow: