PDA

View Full Version : [SOLVED] Format only selected text on textbox userform



emina002
07-06-2016, 02:35 PM
I have a textbox and a certain button in able to make the selected text bold,italic or underlined using HTML tag (just like the way we can format text here in post thread). I am using HTML tag because my objective is to send an email, is like I am composing an email body like GMAIL. The thing is when i tried to format certain text that I select, it seems whole value of textbox adding a HTML tag. Can you help me with it?

See code below.




Private Sub CommandButton3_Click()
TextBox1.SetFocus
TextBox1.Text = "<b>" & TextBox1.Text & "</b>"


End Sub

SamT
07-06-2016, 03:49 PM
VBA is not a HyperText Markup Language. There is no way to use a programming language other than VBA in VBA.

Would you use VBA in a web page?

<P>.Font.Bold = True "This is bold Font" </P>.Font.Bold = False


Private Sub CommandButton3_Click()
With Me.TextBox1
.SetFocus
.Font.Bold = True
End With
End Sub
NB: You can not format only part of the text in a TextBox, it is all or none.


Then in the Email Body String, where you put the TextBox1 text in

myString = "<b>" & UserForm1.TextBox1.Text & "</b>"

emina002
07-06-2016, 04:05 PM
Hi SamT,I will used the content of textbox to outlook and I might sure outlook accept html tag and this tag not visible once I send it. I have also a preview email button transfering the content of textbox to label.

SamT
07-07-2016, 08:11 AM
Then what is the problem?

Kenneth Hobs
07-07-2016, 08:32 AM
This should get you started. Some other code may be needed for: no selection, selection at beginning, selection at end.

Private Sub CommandButton1_Click() Dim s As String
With TextBox1
s = Mid(.Text, 1, .SelStart - 1) & _
"<b>" & .SelText & "</b>" & _
Right(.Text, Len(.Text) - .SelStart - .SelLength)
End With
MsgBox s
End Sub

emina002
07-07-2016, 09:59 AM
Hi Mr Kenneth Hubs, it is indeed what I need but on the last part of your code I don't want a pop up message box.Example is If I type on text box this sentence "Hello World", If I highlight "World" word and press the command button enable to make it bold or somehow add tag like "Hello <b>World</b>", it will add only an HTML tag between then text I selected. Is this possible?

Kenneth Hobs
07-07-2016, 10:53 AM
As Sam told you, partial bold is not possible in that control.
.Text = s

emina002
07-08-2016, 03:42 PM
I figure it out that I cannot format specific text only, but with this code, when I send the entered text to outlook the result is somehow the email body already formatted and not need any other tag. Thanks :)


.HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & AdaptedText & "</font></p>"