how to insert DocVariable into a table cell?

K

Kelie

hello,

this is how i'm doing it now. but seems awkward.

cell.Select
With ActiveDocument.ActiveWindow.Selection
.ExtendMode = True
.EscapeKey
End With
ActiveDocument.Fields.Add ...

can i insert the docvariable without selecting and deselecting the
cell?

thanks.
 
J

Jay Freedman

Not only _can_ you avoid selecting and deselecting, you _should_ avoid it
whenever possible. Instead, declare a Range variable, set it to point to the
particular cell you want, and make that Range object the value of the Range
argument in the Fields.Add statement:

Dim myRg As Range

' point at the cell you want
' in the table you want
Set myRg = ActiveDocument.Tables(1).Cell(2, 2).Range

' exclude table cell marker
myRg.MoveEnd wdCharacter, -1

ActiveDocument.Fields.Add _
Range:=myRg, _
Type:=wdFieldDocVariable, _
Text:="dv1"

ActiveDocument.Fields.Update

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
K

Kelie

Not only _can_ you avoid selecting and deselecting, you _should_ avoid it
whenever possible. Instead, declare a Range variable, set it to point to the
particular cell you want, and make that Range object the value of the Range
argument in the Fields.Add statement:

Dim myRg As Range

' point at the cell you want
' in the table you want
Set myRg = ActiveDocument.Tables(1).Cell(2, 2).Range

' exclude table cell marker
myRg.MoveEnd wdCharacter, -1

ActiveDocument.Fields.Add _
Range:=myRg, _
Type:=wdFieldDocVariable, _
Text:="dv1"

ActiveDocument.Fields.Update

Thanks for your help Jay.
 

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