Word 2013 VBA User Form Textbox Using Bookmarks is duplicating Text into the Word doc that's entered in Textbox RRS feed

  • Question

  • Word 2013: Using a Textbox on a UserForm that is tied to a bookmark. When the text is entered, let's say, 1234, into the Textbox, then in the Word doc it appears as 1234123121

    The code is simple: but I cannot figure out what is going wrong with it.

    Private Sub Txtnew_Change()

    ActiveDocument.Bookmarks("SysNew").Range.Text = Txtnew.Value

    End Sub


    Tuesday, March 31, 2015 7:42 PM

All replies

  • Your code writes the value at the bookmark rather than in it, so it is easy to accumulate values, especially when you are writing to the bookmark each time there is a change to the text field.

    Use the FillBM function at to write the value into the bookmark and replace what was there previously.

    In any case it is better to write values to bookmarks all at once when the userform is closed rather than when there is a change to the text box (there is a change every time you type a character).

    You can do this by associating the command to the userform close button e.g.

    Private Sub CommandButton1_Click()
        FillBM "SysNew", Me.Txtnew.Value
        Exit Sub
    End Sub

    Graham Mayor - Word MVP

    Wednesday, April 1, 2015 6:17 AM
  • You might find it better to use DOCVARIABLE fields rather than bookmarks.

    If you have a { DOCVARIBALE SysNew } field in the document, the following code will populate that field with the data

    With ActiveDocument
        .Variables("SysNew").Value = Txtnew.Value
    End With

    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Monday, April 6, 2015 9:40 AM