• Is it possible to bookmark a Word field using VBA?

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Is it possible to bookmark a Word field using VBA?

    Author
    Topic
    #462558

    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.)

    Viewing 0 reply threads
    Author
    Replies
    • #1177735

      OK, I managed to kludge this workaround. Any comments? Suggestions?


      [indent]Function InsertBookmarkedField(rngLocation As Range, strButtonText As String, Optional 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)
      Set rngLocation = NewField.Result
      rngLocation.MoveStart wdCharacter, -1

      If bmkProperty “” Then ActiveDocument.Bookmarks.Add Name:=bmkProperty, Range:=rngLocation
      Set InsertBookmarkedField = NewField.Result[/indent]
      End Function[/indent]

      (I made the Bookmarking bit optional.)

      • #1177745
        Code:
        	If bmkProperty  "" Then ActiveDocument.Bookmarks.Add Name:=bmkProperty, Range:=rngLocation

        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))
        • #1177750

          Thanks. (The Pennsylvania Dutch in me craves as few lines of code as possible!)

    Viewing 0 reply threads
    Reply To: Is it possible to bookmark a Word field using VBA?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: