|
|
Title | Prefix mail text |
Keywords | prefix mail text, email, break lines |
Categories | Strings |
|
|
This program prefixes text with email reply characters like this:
> This program prefixes text with
> email reply characters like this.
Optionally it can combine lines of text that are short. This is useful if you are
writing simple text, though it is not useful for code and other text where you want
to preserve the existing line breaks.
If we should combine lines, as you would while formatting text, the PrepareMailText function
replaces vbCrLf with spaces in the input text. If we should not combine lines, as you would while
formatting source code, PrepareMailText leaves the vbCrLf's alone.
PrepareMailText then splits the text into strings delimited by carriage returns. It then calls
function PrepareLine to prepare each line.
PrepareLine uses Split to brak the text into words. For each word, it determines whether
the result line is short enough to add the new word to it. If the word would make the result
too long, PrepareLine adds a new line to the result text.
|
|
' Break the input text into lines of length <=
' line_length. Then begin each with prefix.
Private Function PrepareMailText(ByVal txt_in As String, _
ByVal prefix As String, ByVal line_length As Integer, _
ByVal combine_lines As Boolean) As String
Dim result As String
Dim lines() As String
Dim i As Integer
' If we should combine lines, replace vbCrLf with
' spaces.
If combine_lines Then
txt_in = Trim$(Replace(txt_in, vbCrLf, " "))
End If
' Split the text into lines.
lines = Split(txt_in, vbCrLf)
' Process the lines.
For i = LBound(lines, 1) To UBound(lines, 1)
result = result & PrepareLine(lines(i), prefix, _
line_length)
Next i
' Return the result.
PrepareMailText = result
End Function
' Prepare this line.
Private Function PrepareLine(ByVal txt_in As String, ByVal _
prefix As String, ByVal line_length As Integer) As _
String
Dim result As String
Dim words() As String
Dim new_line As String
Dim i As Integer
' Do nothing if the string is blank.
If Len(txt_in) < 1 Then Exit Function
' Break the line into words.
words = Split(txt_in, " ")
' Add the words to the output.
new_line = words(0)
For i = 1 To UBound(words, 1)
' See if the word will fit.
If Len(new_line) + Len(words(i)) >= line_length Then
' It won't fit. Add the new line to the output.
result = result & prefix & new_line & vbCrLf
' Start the new line with the word.
new_line = words(i)
Else
' The word will fit.
new_line = new_line & " " & words(i)
End If
Next i
' Add the last line.
result = result & prefix & new_line & vbCrLf
' Return the result.
PrepareLine = result
End Function
|
|
|
|
|
|