Home > front end >  Preventing from Boomarks getting deleted
Preventing from Boomarks getting deleted

Time:06-06

I have some code I want to run - but the bookmarks in Word keep getting deleted - I want it to "overwrite" the last Input not add it.

   Dim NAME As Range
    Set NAME = ActiveDocument.Bookmarks("Username").Range
    NAME.Text = Me.TextName.Value

    
   Dim Surname As Range
    Set Surname  = ActiveDocument.Bookmarks("UsernameSurname").Range
    Surname.Text = Me.TextSurname.Value

Does anyone have an solution for this? Thanks.

CodePudding user response:

Your code doesn't delete the bookmarks - it simply inserts content after them.

To update the bookmarks, you might use, for example:

Call UpdateBookmark("Username",Me.TextName.Value)
Call UpdateBookmark("UsernameSurname",Me.TextSurname.Value)

coupled with:

Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(StrBkMk) Then
    Set BkMkRng = .Bookmarks(StrBkMk).Range
    BkMkRng.Text = StrTxt
    .Bookmarks.Add StrBkMk, BkMkRng
  End If
End With
Set BkMkRng = Nothing
End Sub
  • Related