Home » excel » excel – Making a word bold in a textbox

excel – Making a word bold in a textbox

Posted by: admin May 14, 2020 Leave a comment

Questions:

New in Stack Overflow, I’m building a macro in Excel with VBA. Basically I have a file with multiple tabs with information in tables, these tables have text in it and some words from that text are bold and repeat in each tab (lets say owner and process). I do display of this information in a textbox located in a form according to the line of the table they pick before on a listbox, the text displays correctly but it ignore the text formatting(bold and italic). Is there a way to display the text formatting in the textbox as it is in the table?

Hope I’ve have made myself clear enough.

How to&Answers:

Typical example for a Shape textbox (not ActiveX):

Sub BoxMaker()
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 217.5, 51#, _
        482.25, 278.25).Select
    Selection.Name = "SPLASH"
    Selection.Characters.Text = "Please Wait for Macro"
    With Selection.Characters(Start:=1, Length:=21).Font
        .Name = "Arial"
        .FontStyle = "Regular"
        .Size = 36
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With

    Selection.Characters(Start:=8, Length:=4).Font.Bold = True

    Selection.HorizontalAlignment = xlCenter
    Selection.VerticalAlignment = xlCenter
End Sub

enter image description here

You can format text in a textbox similar to text in a cell.

Answer:

Possible work around in UserForm using a Webbrowser control

Due to your comment: “…actually I’m using MSForms.TextBox

A possible work around would be to create a simple HTML file using the b tag (e.g. blabla within normal textbold text …) for bold text and load it into a Webbrowser control (needs reference to Microsoft Internet Controls) e.g. via WebBrowser1.Navigate ThisWorkbook.Path & “\topic.htm”.

As HTML commonly uses utf-8 encoding I demonstrate an approach using the system function WideCharToMultiByte (API call).

Example call using helper functions (API calls)

' declare and assign simple html string
  Dim htmlstring as String
  htmlstring = "<html><body><div>Normal text <b>bold text</b> further text</div></body></html>"
' write html file via helper procedure  
  writeUtf8 htmlstring, ThisWorkbook.Path & "\topic.htm"
' load html file into WebBrowswer control
  Me.WebBrowser1.Navigate ThisWorkbook.Path & "\topic.htm"

Helper procedures

I suggest to write a separate code module for these helper functions:

Option Explicit  ' declaration head of separate code module

' Declare API Function
Private Declare Function WideCharToMultiByte Lib "kernel32.dll" ( _
  ByVal CodePage As Long, _
  ByVal dwFlags As Long, _
  ByVal lpWideCharStr As Long, _
  ByVal cchWideChar As Long, _
  ByVal lpMultiByteStr As Long, _
  ByVal cbMultiByte As Long, _
  ByVal lpDefaultChar As Long, _
  ByVal lpUsedDefaultChar As Long) As Long


Public Sub writeUtf8(ByVal s As String, ByVal datei As String)
' Purpose: save HTML String in utf-8 mode 
' Note:    called by  writeUtf8 htmlstring, thisworkbook.path & "\topic.htm"
Dim file As Integer
Dim B() As Byte
  file = FreeFile
  Open datei For Binary Access Write Lock Read Write As #file
  getUtf8 s, B
  Put #file, , B
  Close #file
End Sub

Private Sub getUtf8(ByRef s As String, ByRef B() As Byte)
' Note: called by above helper function; uses API call (see declaration head)
Const CP_UTF8 As Long = 65001
Dim len_s As Long
Dim ptr_s As Long
Dim size As Long
  Erase B
  len_s = Len(s)
  If len_s = 0 Then _
    err.Raise 30030, , "Len(WideChars) = 0"
  ptr_s = StrPtr(s)
  size = WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, 0, 0, 0, 0)
  If size = 0 Then _
    err.Raise 30030, , "WideCharToMultiByte() = 0"
  ReDim B(0 To size - 1)
  If WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, VarPtr(B(0)), size, 0, 0) = 0 Then _
    err.Raise 30030, , "WideCharToMultiByte(" & Format$(size) & ") = 0"
End Sub