Consulting

Results 1 to 3 of 3

Thread: How to only use value from textbox if value exists

  1. #1
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location

    How to only use value from textbox if value exists

    Hi. I have a document where i use textboxes from a userform to write to bookmarks in a word-document.
    This is the code that run when i click a button:

    If ThisDocument.Bookmarks.Exists("BM_Fastighetsbeteckning") = True Then
    Dim BM_Fastighetsbeteckning As Range
    Set BM_Fastighetsbeteckning = ThisDocument.Bookmarks("BM_Fastighetsbeteckning").Range
    BM_Fastighetsbeteckning.Text = Me.TextBox1.Value
    ThisDocument.Bookmarks.Add "BM_Fastighetsbeteckning", BM_Fastighetsbeteckning
    End If

    It works and is does not try to write if the bookmark is missing (i have many other textboxes and hundreds of bookmarks). This is used to generate the document but the problem is when there is no value in the textbox. Then it removes the text from the bookmark (since there is no text in the textbox). I also use this to revise already generated documents and i donīt want the existing data in the document to be deleted.
    How do i make it so that it only use the textbox if i have written something in the textbox?
    I tried this but it does nothing:

    If Me.TextBox1.Value = True Then
    If ThisDocument.Bookmarks.Exists("BM_Fastighetsbeteckning") = True Then
    Dim BM_Fastighetsbeteckning As Range
    Set BM_Fastighetsbeteckning = ThisDocument.Bookmarks("BM_Fastighetsbeteckning").Range
    BM_Fastighetsbeteckning.Text = Me.TextBox1.Value
    ThisDocument.Bookmarks.Add "BM_Fastighetsbeteckning", BM_Fastighetsbeteckning
    End If
    End If

    What am i doing wrong?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You might try:

    If Not TextBox1 = vbNullString Then

    End If
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    Thanks, Greg, that did the trick

    This is the finshed code:

    If Not TextBox1 = vbNullString Then
        If ThisDocument.Bookmarks.Exists("BM_Fastighetsbeteckning") = True Then
        Dim BM_Fastighetsbeteckning As Range
        Set BM_Fastighetsbeteckning = ThisDocument.Bookmarks("BM_Fastighetsbeteckning").Range
        BM_Fastighetsbeteckning.Text = Me.TextBox1.Value
        ThisDocument.Bookmarks.Add "BM_Fastighetsbeteckning", BM_Fastighetsbeteckning
        End If
        End If
    Last edited by Paul_Hossler; 12-29-2020 at 08:35 PM.

Posting Permissions

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