Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.)

    Function InsertBookmarkedField(rngLocation As Range, strButtonText As String, bmkProperty As String) As Range
    Dim NewField As Field
    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
    End Function
    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.)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I managed to kludge this workaround. Any comments? Suggestions?


    Function InsertBookmarkedField(rngLocation As Range, strButtonText As String, Optional bmkProperty As String) As Range
    Dim NewField As Field
    Set NewField = rngLocation.Fields.Add(Range:=rngLocation, Text:="MACROBUTTON nomacro " & strButtonText, PreserveFormatting:=False)
    NewField.Code.Text = Trim(NewField.Code.Text)
    Set rngLocation = NewField.Result
    rngLocation.MoveStart wdCharacter, -1

    If bmkProperty <> "" Then ActiveDocument.Bookmarks.Add Name:=bmkProperty, Range:=rngLocation
    Set InsertBookmarkedField = NewField.Result
    End Function


    (I made the Bookmarking bit optional.)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='Mesaka' post='793767' date='17-Sep-2009 12:00']
    Code:
    	If bmkProperty <> "" Then ActiveDocument.Bookmarks.Add Name:=bmkProperty, Range:=rngLocation
    [/quote]
    Try something like this (note the difference in the Start):
    Code:
    ActiveDocument.Bookmarks.Add Name:="bmkProperty", _
    	Range:=ActiveDocument.Range(Start:=NewField.Code.Start, End:=NewField.Result.End))

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. (The Pennsylvania Dutch in me craves as few lines of code as possible!)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •