OK, I’m at wit’s end with this one. I’m trying to write a function that will create a field and then bookmark it. The catch is I don’t want to involve the “selection”, I just want to use ranges. The problem is that I can’t figure out how on earth to reference the field as a range. Using the .Result property returns the insertion point AFTER the field, which is completely useless (and somewhat counter to what the “help” file indicates.)
Here is the code. Note that it takes three required arguments (where to insert the field, what text to display in the field, and the name of the bookmark to apply.)
[indent]Function InsertBookmarkedField(rngLocation As Range, strButtonText As String, bmkProperty As String) As Range
Dim NewField As Field
[indent] Set NewField = rngLocation.Fields.Add(Range:=rngLocation, Text:=”MACROBUTTON nomacro ” & strButtonText, PreserveFormatting:=False)
NewField.Code.Text = Trim(NewField.Code.Text) ‘get rid of the trailing space
Set rngLocation = NewField.Result
ActiveDocument.Bookmarks.Add Name:=bmkProperty, Range:=rngLocation
Set InsertBookmarkedField = NewField.Result ‘this obviously doesn’t work, since it’s returning the insertion point immediately AFTER the field[/indent]
End Function
[/indent]
I’ve tried several other ways to reference the range occupied by the field, including such kludges as rngLocation.Paragraphs(1).Range.Fields(1).Result, but all to no avail. Is there any hope, without involving the Select method?
(By the way, this is in Word 2007, if that makes any difference.)