Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jan 2001
    Location
    Barbados, West Indies
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assigning text to bookmarks

    Hi all,

    Word 97/2000

    I am designing a template with a dialog which takes input from a user, e.g. a name, assigns it to a variable, then puts that variable into a bookmark in the active document. That bookmarks text is then to be repeated throughtout the document using REF fields.

    This is what I've got so far:

    Private Sub CommandButton1_Click()
    Dim NameOfPerson As String

    NameOfPerson = TextBox1.Text

    'Get rid of the dialog

    Unload UserForm1

    ActiveDocument.Bookmarks("name").Range.Text = NameOfPerson

    End Sub

    Trouble is, the code which assigns the input to the bookmark seems to overwrite the bookmark, meaning I can't use the REF field to access. Obviously I am going wrong somewhere, but where? All advice appreciated.

    Thanks,

    Phil

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Assigning text to bookmarks

    Hi Barke,

    You're not doing anything wrong: deleting the bookmark is the default behavior when you use the code you've posted. Often that is just fine, but not always.

    In those cases you need to use an "InsertButKeepBookmark" macro. There are a number of these floating around. Here's <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/InsertingTextAtBookmark.htm>a link </A>to an article by Dave Rado on the MVPS site, that contains a code solution.

    Gary

  3. #3
    New Lounger
    Join Date
    Jan 2001
    Location
    Barbados, West Indies
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Assigning text to bookmarks

    Thanks Gary, I suspected this was an FAQ!

    Phil

  4. #4
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Assigning text to bookmarks

    Here's a method to use:

    Public Sub WriteBookmark(BookmarkName As String, Value As String, Optional KeepBookmark As Boolean = True, Optional KeepSelected As Boolean = False)

    With ActiveDocument

    If .Bookmarks.Exists(BookmarkName) Then

    .Bookmarks(BookmarkName).Select
    Selection.Text = Value

    If KeepBookmark = True Then
    .Bookmarks.Add BookmarkName, Selection.Range
    End If

    If KeepSelected = False Then
    Selection.Collapse wdCollapseEnd
    End If

    End If

    End Sub

    You can set your defaults any way you wish, or not specify them at all when calling the subroutine.

    Feel free to e-mail questions.

Posting Permissions

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