Log in

View Full Version : [SOLVED:] Inserting text from VBA textbox into part of a string



FmsBlnkr
12-02-2022, 04:33 AM
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!

macropod
12-04-2022, 08:23 PM
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

macropod
12-04-2022, 08:29 PM
Cross-posted at: ms word - Inserting text from VBA UserForm textbox into part of a string - Stack Overflow (https://stackoverflow.com/questions/74674790/inserting-text-from-vba-userform-textbox-into-part-of-a-string)
Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

FmsBlnkr
12-05-2022, 01:04 AM
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!

Aussiebear
12-05-2022, 01:09 AM
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.

FmsBlnkr
12-05-2022, 01:13 AM
I'm very new to VBA and these style of forums, so apologies again. Lesson learnt.