Consulting

Results 1 to 8 of 8

Thread: Format only selected text on textbox userform

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    17
    Location

    Format only selected text on textbox userform

    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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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>"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    17
    Location
    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.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Then what is the problem?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

  6. #6
    VBAX Regular
    Joined
    May 2016
    Posts
    17
    Location
    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?

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    As Sam told you, partial bold is not possible in that control.
    .Text = s
    Last edited by Kenneth Hobs; 07-07-2016 at 11:11 AM.

  8. #8
    VBAX Regular
    Joined
    May 2016
    Posts
    17
    Location
    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>"

Posting Permissions

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