Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing bookmark text (Word 97)

    OK I'll preface this by saying I'm not a VBA expert - I get by through sheer stubborness, rather than any great skill or knowledge...
    The scenario is this: I have a template with a dialog box that opens when a document based on the template is created. The information entered into the dialog box populates bookmarks in the document. From time-to-time, the information in the bookmarks needs to be updated (eg. the version information needs to be updated). So I've created an Update dialog box and even managed to get the existing bookmark information to populate its fields. When I click OK the new information is inserted into the bookmark. Yipee.
    The problem: The old text in the bookmark is not deleted...so I end up with the old information AND the new information in the bookmark.

    To initially populate the bookmarks I've used the following code:
    .Bookmarks("RFTName").Range.InsertBefore (txtRFTName)

    To populate the Update dialog box I've used the following code:
    txtUpdateRFTName.Value = ActiveDocument.Bookmarks("RFTName").Range

    In my umpteenth attempt to repopulate the bookmarks with the new information I'm stuck on:
    .Bookmarks("RFTName").Range.InsertBefore (txtUpdateRFTName)

    Any help would be enormously appreciated. This is driving me mad.
    Karen

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

    Re: Replacing bookmark text (Word 97)

    Karen,

    There are a number of routines around for doing this; here's one I was able to quickly find in my files - don't remember where I got it from so can't provide credit where due...

    <pre>Public Function InsertWithBookmarks(strBmkName As String, _
    strInsertText As String) _
    As Boolean
    Dim rngBmk As Range
    'First test that bookmark exists
    If ActiveDocument.Bookmarks.Exists(strBmkName) Then
    Set rngBmk = ActiveDocument.Bookmarks(strBmkName).Range
    rngBmk.Text = strInsertText
    'this overwrites the bookmark, so need to add it again:
    ActiveDocument.Bookmarks.Add Name:=strBmkName, Range:=rngBmk
    Set rngBmk = Nothing
    InsertWithBookmarks = True
    Else
    MsgBox strBmkName & " bookmark doesn't exist in this document. "
    End If

    End Function
    '#####################
    Public Sub Testing()

    InsertWithBookmarks "bmkTest", "Text To Insert"

    End Sub
    </pre>

    Gary

  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

    Re: Replacing bookmark text (Word 97)

    > In my umpteenth attempt to repopulate the bookmarks with the new information I'm stuck on:
    > .Bookmarks("RFTName").Range.InsertBefore (txtUpdateRFTName)

    I'm fairly certain this will not disturb the original bookmark contents. You either could delete those contents using a method appropriate to a range object, or try to replace it using a select/paste approach.

    (Going away to test these ideas...)

    ActiveDocument.Bookmarks("bmk1").Range.Delete > BAD, DELETES THE BOOKMARK ITSELF

    ActiveDocument.Bookmarks("bmk1").Range.Text = vbNullString > SAME

    ActiveDocument.Bookmarks("bmk1").Range.Text = "Hi Mom" > ALSO CLEARS BOOKMARK NAME

    I don't know how this is supposed to be done, officially, I bet there is an easier method over at mvps.org, but this does work:

    <pre>Dim strNewText As String
    strNewText = "This is a test"
    Dim intBmkLength As Integer, rngTemp As Range
    Set rngTemp = ActiveDocument.Bookmarks("bmk1").Range
    With rngTemp
    'store length of BMK text before inserting
    intBmkLength = .End - .Start
    .InsertBefore strNewText
    'redefine range to the "old" contents
    .SetRange .End - intBmkLength, .End
    .Delete
    End With
    Set rngTemp = Nothing</pre>

    Hope this helps.

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replacing bookmark text (Word 97)

    FANTASTIC! <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35>
    In the end I was wary of allowing the bookmark to be deleted and then having to recreate it and went for this solution. It worked beautifully.
    Many thanks to you both for your help. There's NO WAY stubbornness alone would have helped me find this code.
    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

Posting Permissions

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