Replace numbers in email body

前端 未结 1 1471
盖世英雄少女心
盖世英雄少女心 2021-01-07 14:08

i have some numbers with point after them in emails. The problem is that they are descending in the email: 6. 5. 4. 3. 2. 1. - first after hello is 6. 5. 4. 3. 2. 1. B

相关标签:
1条回答
  • 2021-01-07 14:25

    Your request is very vage. This is due to the fact that you want to parse a string which you haven't properly "defined". The missing pieces are:

    (1) What makes a line a line in your string? Are they separated with a line break such as vbCrLf or are you using Chr(10) or anything else instead? Can it be all of them interchangeably?

    (2) How do you want to tell VBA that a list starts and that a list ends? Is it that there is a number as the first character in a line? What if a line starts with a number due to other reasons or if someone put a space before that number such as 2. some thing to be done. Then the first character would be a space and not a number. Especially, if your list surpasses 10 items it might be that the first 9 number will be written as 1. (with a leading space).

    (3) Once the list is completed no more list items should be generated and the list should be marked "closed". How do you define this? Is it that there were lines starting with a number before and that the next line does not start with a number anymore? But that would also mean that nobody can use a Return within any of the line items (because the description of what should be done is getting too long).

    Since none of the above is included in your question, a reader might come to the conclusion that you haven't really fully understood the problem yet. I guess that explains also the comment by @CindyMeister.

    Still, I had a little bit of a spare time and gave it some thought and came up with the following small sub:

    Option Explicit
    
    Public Sub HTML_List()
    
    Dim lngRow As Long
    Dim lngChar As Long
    Dim strTmp() As String
    Dim strHTMLbody As String
    Dim bolListStart As Boolean
    
    strHTMLbody = "Hello,  " & vbCrLf
    strHTMLbody = strHTMLbody & " " & vbCrLf
    strHTMLbody = strHTMLbody & "6. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & "5. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & "4. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & "3. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & "2. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & "1. some thing to be done " & vbCrLf
    strHTMLbody = strHTMLbody & " " & vbCrLf
    strHTMLbody = strHTMLbody & "Have a nice day. "
    
    strTmp() = Split(strHTMLbody, vbCrLf)
    
    For lngRow = LBound(strTmp) To UBound(strTmp)
        strTmp(lngRow) = Trim(strTmp(lngRow))
        If strTmp(lngRow) <> vbNullString Then
            Select Case Asc(strTmp(lngRow))
            Case 48 To 57
                strTmp(lngRow) = Mid(strTmp(lngRow), InStr(1, strTmp(lngRow), ".", vbTextCompare) + 1)
                strTmp(lngRow) = "<li>" & strTmp(lngRow) & "</li>"
                If bolListStart = False Then
                    strTmp(lngRow) = "<ol>" & strTmp(lngRow)
                    bolListStart = True
                End If
            Case Else
                If bolListStart = True Then
                    bolListStart = False
                    strTmp(lngRow - 1) = strTmp(lngRow - 1) & "</ol>"
                End If
            End Select
        End If
    Next lngRow
    
    strHTMLbody = ""
    For lngRow = LBound(strTmp) To UBound(strTmp)
        strHTMLbody = strHTMLbody & strTmp(lngRow) & vbCrLf
    Next lngRow
    
    Debug.Print strHTMLbody
    
    End Sub
    

    So, the basic steps behind the sub are the following:

    1. Split the initial string by vbCrLf (hopefully that makes a line a line).
    2. Check if the first character in that line is a number.
    3. With the first occurence of the above a list will be started. In HTML a list is started with <ol>.
    4. Remove the number from that line and rather tell HTML that this is a list item. That's tagged with a <li> at the start of a list item and a </li> at the end of that line item.
    5. Once, a line does not start anymore with a number, the list is closed in HTML with </ol>.
    6. Bring all the lines together again into one HTML string.

    If you use then this string in an Outlook email as .HTMLbody then HTML will automatically format your list and number the line items accordingly.

    As a result of the above code you will get the following in return:

    Hello,
    
    <ol><li> some thing to be done</li>
    <li> some thing to be done</li>
    <li> some thing to be done</li>
    <li> some thing to be done</li>
    <li> some thing to be done</li>
    <li> some thing to be done</li>
    </ol>
    Have a nice day.

    I hope that helps. Let me know if you have any questions.

    0 讨论(0)
提交回复
热议问题