更换电子邮件正文中的数字 [英] Replace numbers in email body

查看:133
本文介绍了更换电子邮件正文中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在电子邮件中我有一些数字。问题是他们在电子邮件中下降:6. 5. 4. 3. 2. 1. - 第一次你好,是6. 5. 4. 3. 2. 1.但不总是有时只有3或4或其中2个:2. 1.或4. 3. 2. 1.有没有办法命令他们上升后你好:1. 2. 3. 4. 5. 6.通过vba?该文本是纯文本编号,只有6个数字,在他们的电子邮件中。数字不会自动生成。



ex:

 您好,

6.一些事情要完成
5.一些事情要完成
4.有些事情要完成
3.有些事情要完成
2.有些事情要做要完成
1.有些事情要做

有一个愉快的一天。

 你好,

4.有些东西要完成
3.有些事情要完成
2.有些事情要完成
1.一些要完成的事情

有一个美好的一天。

自动段落不起作用:

  *你好,
*
4.有些事情要完成
3.有些事要完成
2.有些事要做完成
1.有些事情要做
*
*有一个愉快的一天。

我已经尝试过了,但没有起作用。

  With objItem.Content.Find 
.Text =6.
.Forward = True
.Execute

如果发现= True然后
objItem.HTMLBody = Replace(objItem.HTMLBody,6.,1。 )
objItem.HTMLBody = Replace(objItem.HTMLBody,5.,2.)
objItem.HTMLBody = Replace(objItem.HTMLBody,4.,3.)
objItem.HTMLBody = Replace(objItem.HTMLBody,3.,4.)
objItem.HTMLBody =替换(objItem.HTMLBody,2.,5.)
objItem.HTMLBody = Replace(objItem.HTMLBody,1.,6.)

Else

带objItem.Content.Find
。 Text =5.
.Forward = True
.Execute

如果发现= True则
objItem.HTMLBody = Replace(objItem.HTMLBody,5.,1。 )
objItem.HTMLBody = Replace(objItem.HTMLBody,4.,2.)
objItem.HTMLBody = Replace(objItem.HTMLBody,2.,4.)
objItem.HTMLBody = Replace(objItem.HTMLBody,1.,5.)

'Else ...

结束

结束


解决方案

您的请求非常vage。这是因为你想解析一个你没有正确定义的字符串。缺少的部分是:



(1)你的字符串中的一行是什么?他们分开了一个换行符,如 vbCrLf 还是使用 Chr(10)或其他任何东西?可以互换吗?



(2)你想如何告诉VBA列表开始,列表结束?是否有一个数字作为一行中的第一个字符?如果由于其他原因,如果一个行以数字开头,或者有人在该号码之前放置了一个空格,例如 2。一些事情要完成。那么第一个字符将是空格而不是数字。特别是,如果您的列表超过10个项目,可能是前9个数字将被写为 1。(带有前导空格)。



(3)一旦列表完成,就不应该生成更多的列表项,列表应该被标记为关闭。你如何定义这个?是否有一行以数字开头,下一行不是以数字开头的?但这也意味着没有人可以在任何订单项中使用 Return (因为应该做的事情的描述太长)。



由于上述内容都不包括在您的问题中,读者可能会得出结论,您还没有完全了解问题。我想这也解释了@CindyMeister的评论。



然而,我有一点空余时间,并给了一些想法,并提出了以下小子:

  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 =你好, &安培; vbCrLf
strHTMLbody = strHTMLbody& & vbCrLf
strHTMLbody = strHTMLbody& 6.要做的事& vbCrLf
strHTMLbody = strHTMLbody& 5.有事要做& vbCrLf
strHTMLbody = strHTMLbody& 4.有事要做& vbCrLf
strHTMLbody = strHTMLbody& 有些事要做& vbCrLf
strHTMLbody = strHTMLbody& 2.要做的事& vbCrLf
strHTMLbody = strHTMLbody& 一些事情要做& vbCrLf
strHTMLbody = strHTMLbody& & vbCrLf
strHTMLbody = strHTMLbody&

strTmp()= Split(strHTMLbody,vbCrLf)

对于lngRow = LBound(strTmp)到UBound(strTmp)
strTmp (lngRow)= Trim(strTmp(lngRow))
如果strTmp(lngRow)<> vbNullString然后
选择案例Asc(strTmp(lngRow))
案例48到57
strTmp(lngRow)=中(strTmp(lngRow),InStr(1,strTmp(lngRow))。 ,vbTextCompare)+ 1)
strTmp(lngRow)=< li> &安培; strTmp(lngRow)& < /锂> 中
如果bolListStart = False然后
strTmp(lngRow)=< ol> &安培; strTmp(lngRow)
bolListStart = True
End If
Case Else
如果bolListStart = True然后
bolListStart = False
strTmp(lngRow - 1)= strTmp(lngRow - 1)& < /醇> 中
End If
End Select
End If
Next lngRow

strHTMLbody =
对于lngRow = LBound(strTmp)到UBound strTmp)
strHTMLbody = strHTMLbody& strTmp(lngRow)& vbCrLf
下一页lngRow

Debug.Print strHTMLbody

End Sub

所以,sub的背后的基本步骤如下:


  1. 将初始字符串拆分为 vbCrLf (希望这是一条线)

  2. 检查该行中的第一个字符是否是一个数字。

  3. 随着上面的第一次出现,列表将被启动。在HTML中,列表以< ol> 开始。

  4. 删除该行的数字,而是告诉HTML这是一个列表项。在列表项目的起始处标有一个< li> ,在< / li>

  5. 一次,一行不再有一个数字,列表在HTML中以< / ol>

  6. 将所有行重新合并成一个HTML字符串。

如果您在Outlook电子邮件中使用此字符串作为 .HTMLbody ,则HTML将自动格式化您的列表并相应地对行项目进行编号。



作为上述代码的结果,您将得到以下结果:



  Hello,< ol>< li& ;一些事情要完成< / li>< li>一些事情要完成< / li>< li>一些事情要完成< / li>< li>一些事情要完成< / li>< li>一些事情要完成< / li>< li>一些事情要做< / li>< / ol>有一个美好的一天。 



我希望有帮助。如果您有任何问题,请告知我。


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. But not always, sometimes are just 3 or 4 or 2 of them: 2. 1. or 4. 3. 2. 1. Is there a way to order them ascending after hello: 1. 2. 3. 4. 5. 6. via vba ? The text is plain text numbers and are only 6 numbers with point after them in the email. The numbers are not auto generated.

ex:

Hello, 

6. some thing to be done
5. some thing to be done
4. some thing to be done
3. some thing to be done
2. some thing to be done
1. some thing to be done

Have a nice day.

or

Hello, 

4. some thing to be done
3. some thing to be done
2. some thing to be done
1. some thing to be done

Have a nice day.

The auto paragraph is not working :

   * Hello, 
   * 
    4. some thing to be done
    3. some thing to be done
    2. some thing to be done
    1. some thing to be done
   * 
   * Have a nice day.

i have tried this but it doesn't work.

With objItem.Content.Find
    .Text = "6."
    .Forward = True
    .Execute

    If .Found = True Then
    objItem.HTMLBody = Replace(objItem.HTMLBody, "6.", "1.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "5.", "2.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "4.", "3.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "3.", "4.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "2.", "5.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "1.", "6.")

    Else

    With objItem.Content.Find
    .Text = "5."
    .Forward = True
    .Execute

    If .Found = True Then
    objItem.HTMLBody = Replace(objItem.HTMLBody, "5.", "1.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "4.", "2.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "2.", "4.")
    objItem.HTMLBody = Replace(objItem.HTMLBody, "1.", "5.")

    'Else...

    End With

End With

解决方案

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.

这篇关于更换电子邮件正文中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆