Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleBreak text into lines and prefix them for mailing
Keywordsmail, prefix, reply
CategoriesOffice, Utilities
 
At times I've had three-way email discussions and I found it was much easier to tell who said what if I put each person's initials in front of each line of old message. For example:

    R> Do you think this is a good idea?
    B> Yes, if it's cheap enough.
    I disagree.

To make this easier, I wrote this program. Enter text in the upper TextBox. Enter the prefix you want put in front of each line, and the maximum line length (not counting the prefix). Then click the Prefix button. The program breaks the text into lines, prefixes them, and displays the result so you can copy and paste it into your mail system.

Subroutine PrepareMailText uses Split to separate the lines delimited by carriage returns. For each line, it then calls subroutine PrepareLine.

PrepareLine braks the line into words and places as many as will fit on each line, leaving room for the prefix string.

 
Private Sub cmdPrefix_Click()
    txtOutput.Text = PrepareMailText( _
        txtInput.Text, txtPrefix.Text, _
        CInt(txtLineLength.Text), _
        chkCombineLines.Value = vbChecked)
End Sub

' 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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated