Option Explicit
Private Sub Command1_Click()
Call TextBold(RichTextBox1)
Call TextColor(RichTextBox1, vbRed)
Call TextCenter(RichTextBox1)
Call TextRight(RichTextBox1)
Call TextItalics(RichTextBox1)
End Sub
Private Sub Command2_Click()
'When I click on the button I lose all formatting
With RichTextBox1
.Refresh
.Text = Replace(.Text, "<", "")
.Text = Replace(.Text, ">", "")
.Text = Replace(.Text, "(", "")
.Text = Replace(.Text, ")", "")
.Text = Replace(.Text, "{", "")
.Text = Replace(.Text, "}", "")
.Refresh
End With
End Sub
Private Sub Form_Load()
With RichTextBox1
.Text = "<1>" & vbNewLine 'Color these Characters Blue
.Text = .Text & "<(5-6-58)>" & vbNewLine 'Bold These Characters & Center Align
.Text = .Text & "{$1-23-88%}" & vbNewLine 'Italicizes These Characters & Right Align
End With
End Sub
Private Sub TextBold(RTB As RichTextBox)
Dim lngTag_Start As Long
Dim lngTag_End As Integer
Dim strText As String
lngTag_End = 1
With RTB
strText = .Text
Do While InStr(lngTag_End, strText, "<") > 0
.SelLength = 0
lngTag_Start = InStr(lngTag_End, strText, "<")
lngTag_End = InStr(lngTag_Start, strText, ">")
.SelStart = lngTag_Start
.SelLength = (lngTag_End - lngTag_Start) - 1
.SelBold = True
lngTag_End = lngTag_End + 1
Loop
End With
End Sub
Private Sub TextCenter(RTB As RichTextBox)
Dim lngTag_Start As Long
Dim lngTag_End As Integer
Dim strText As String
lngTag_End = 1
With RTB
strText = .Text
Do While InStr(lngTag_End, strText, "(") > 0
.SelLength = 0
lngTag_Start = InStr(lngTag_End, strText, "(")
lngTag_End = InStr(lngTag_Start, strText, ")")
.SelStart = lngTag_Start
.SelLength = (lngTag_End - lngTag_Start) - 1
.SelAlignment = rtfCenter
lngTag_End = lngTag_End + 1
Loop
End With
End Sub
Private Sub TextColor(RTB As RichTextBox, lngColor As Long)
Dim lngTag_Start As Long
Dim lngTag_End As Integer
Dim strText As String
lngTag_End = 1
With RTB
strText = .Text
Do While InStr(lngTag_End, strText, "<") > 0
.SelLength = 0
lngTag_Start = InStr(lngTag_End, strText, "<")
lngTag_End = InStr(lngTag_Start, strText, ">")
.SelStart = lngTag_Start
.SelLength = (lngTag_End - lngTag_Start) - 1
.SelColor = lngColor
lngTag_End = lngTag_End + 1
Loop
End With
End Sub
Private Sub TextRight(RTB As RichTextBox)
Dim lngTag_Start As Long
Dim lngTag_End As Integer
Dim strText As String
lngTag_End = 1
With RTB
strText = .Text
Do While InStr(lngTag_End, strText, "$") > 0
.SelLength = 0
lngTag_Start = InStr(lngTag_End, strText, "$")
lngTag_End = InStr(lngTag_Start, strText, "%")
.SelStart = lngTag_Start
.SelLength = (lngTag_End - lngTag_Start) - 1
.SelAlignment = rtfRight
lngTag_End = lngTag_End + 1
Loop
End With
End Sub
Private Sub TextItalics(RTB As RichTextBox)
Dim lngTag_Start As Long
Dim lngTag_End As Integer
Dim strText As String
lngTag_End = 1
With RTB
strText = .Text
Do While InStr(lngTag_End, strText, "{") > 0
.SelLength = 0
lngTag_Start = InStr(lngTag_End, strText, "{")
lngTag_End = InStr(lngTag_Start, strText, "}")
.SelStart = lngTag_Start
.SelLength = (lngTag_End - lngTag_Start) - 1
.SelItalic = True
lngTag_End = lngTag_End + 1
Loop
End With
End Sub