Consulting

Results 1 to 6 of 6

Thread: Inserting text from VBA textbox into part of a string

  1. #1
    VBAX Newbie
    Joined
    Nov 2022
    Posts
    3
    Location

    Question Inserting text from VBA textbox into part of a string

    Hi there, I'm new to VBA and hoping someone could help, if this might even be possible.

    A date will be manually added by the user into TEXTBOX1.

    .Bookmarks("BOOKMARK1").Range _
    .InsertBefore TEXTBOX1

    I have option buttons for the user to select which will place specific text and formatting (depending on the button selected) into the document as follows:

    Private Sub OptionButton2_Click()
    If Me.OptionButton2.Value = True Then
       Set oRng = ActiveDocument.Bookmarks("BOOKMARK2").Range
       oRng.Text = "EXAMPLE SENTENCE 1" & Chr(11) & Chr(9) & _
                          "EXAMPLE SENTENCE 2" & Chr(11) & _
                          "EXAMPLE SENTENCE 3" & vbNewLine & " "
       ActiveDocument.Bookmarks.Add "BOOKMARK2", oRng
      End If
    End Sub
    I am trying to get the date that has been entered in TEXTBOX1 to appear at the end of the sentence of EXAMPLE SENTENCE 2. Can anybody please help with this? Thank you!
    Last edited by Aussiebear; 12-02-2022 at 05:35 AM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quite simple really. For example:
    Private Sub CommandButton1_Click() 
    Dim Rng As Range, StrOut As String
    If Me.OptionButton2.Value = True Then
        StrOut = "EXAMPLE SENTENCE 1" & Chr(11) & vbTab & _
                "EXAMPLE SENTENCE 2" & Chr(11) & _
                "EXAMPLE SENTENCE 3" & vbCr & " " & Me.TextBox1.Text
    Else
        StrOut = ""
    End If
    With ActiveDocument
        Set Rng = .Bookmarks("BOOKMARK2").Range
        Rng.Text = StrOut
        .Bookmarks.Add "BOOKMARK2", Rng
    End With
    End Sub
    More flexibly:
    Private Sub CommandButton1_Click()
    Dim BkMk As String, StrTxt As String
    Call UpdateBookmark("BOOKMARK1", Me.TextBox1.Text)
    If Me.OptionButton2.Value = True Then
      StrTxt = "EXAMPLE SENTENCE 1" & Chr(11) & vbTab & _
                "EXAMPLE SENTENCE 2" & Chr(11) & _
                "EXAMPLE SENTENCE 3" & vbCr & " " & Me.TextBox1.Text
    Else
      StrTxt = ""
    End If
    Call UpdateBookmark("BOOKMARK2", StrTxt)
    End Sub
    
    
    Sub UpdateBookmark(BkMk As String, StrTxt As String)
    Dim Rng As Range
    With ActiveDocument
      Set Rng = .Bookmarks(BkMk).Range
      Rng.Text = StrTxt
      .Bookmarks.Add BkMk, Rng
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Newbie
    Joined
    Nov 2022
    Posts
    3
    Location
    Sincere apologies about the cross-post, I wasn't aware of this rule however, I've read the policy now. No disrespect to any volunteers was intended.

    Thank you as well for your help! The simple way works perfectly for me and I'll keep it that way until I become more fluent in VBA and then I'll dabble into understanding the other method. Thanks again!

  5. #5
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Quote Originally Posted by FmsBlnkr View Post
    Sincere apologies about the cross-post, I wasn't aware of this rule however, I've read the policy now. No disrespect to any volunteers was intended.
    Its a very common rule amongst the forums.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Newbie
    Joined
    Nov 2022
    Posts
    3
    Location
    I'm very new to VBA and these style of forums, so apologies again. Lesson learnt.

Tags for this Thread

Posting Permissions

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