I am able to add a field to my document at the specified range.
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("test").Range
rng.Fields.Add Range:=rng, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY " & "Author", PreserveFormatting:=False
After this code executes, rng is before (to the left of) the newly added field. I want to move the range so that it is AFTER the newly added field so that I can continue adding text, add another field, etc. I want to do this without using the Selection object.
I've managed to to this by adding a a space and a temporary bookmark after rng and collapse back to the start of rng; I then add the field at rng, and re-set rng at the temporary bookmark, which I then delete. This is awkward, and I'm wondering if there's a more efficient way to move the range object so that it's on the other side of a newly added field.
Thanks.
Richard Barrett
Range / Field
-
- Administrator
- Posts: 78647
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Range / Field
A belated welcome to Eileen's Lounge!
Try this:
Try this:
Code: Select all
Dim rng As Range
Dim fld As Field
Set rng = ActiveDocument.Bookmarks("test").Range
' Create field and assign to variable
Set fld = rng.Fields.Add(Range:=rng, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY " & "Author", PreserveFormatting:=False)
' Set rng to the range of the field
Set rng = fld.Result
' Collapse the range to its end
rng.Collapse Direction:=wdCollapseEnd
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 8
- Joined: 07 Feb 2010, 22:39
Re: Range / Field
Thank you. Yes, of course... I never thought of assigning Fields.Add to an object.