VBA Italic

kitty77

Registered User.
Local time
Today, 16:24
Joined
May 27, 2019
Messages
715
How can I make the word "Red" be italic?

[Mtestitems] = "abc123" & vbCrLf & "Red Apple"
 
Here a little function to format a word in a string with example.

Code:
Public Enum RT_FormatType
  RT_Bold = 1
  RT_Underline = 2
  RT_Italic = 3
  RT_Red = 4
  RT_Blue = 5
  RT_BoldUnderline = 6
  RT_YellowHighlight = 7
End Enum

Public Function FormatWord(searchIn As String, searchFor As String, FormatType As RT_FormatType)
  Dim replaceText As String
  Select Case FormatType
  Case RT_Bold
        replaceText = "<strong>" & searchFor & "</strong>"
     Case RT_Underline
        replaceText = "<u>" & searchFor & "</u>"
     Case RT_Italic
        replaceText = "<em>" & searchFor & "</em>"
     Case RT_Red
        replaceText = "<font color=red>" & searchFor & "</font>"
     Case RT_Blue
        replaceText = "<font color=Blue>" & searchFor & "</font>"
     Case RT_BoldUnderline
        replaceText = "<strong>" & searchFor & "</strong>"
        replaceText = "<u>" & replaceText & "</u>"
     Case RT_YellowHighlight
        replaceText = "<font style= 'BACKGROUND-COLOR:#FFFF00'>" & searchFor & "</font>"
     Case Else
        replaceText = searchFor
   End Select
  searchIn = Replace(searchIn, searchFor, replaceText)
  FormatWord = searchIn
End Function

Public Sub TestFormat()
  Debug.Print FormatWord("dog", "The dog ran", RT_Bold)
End Sub
 

Users who are viewing this thread

Back
Top Bottom