VBA Outlook - Replace Paragraph Mark With Manual Line Break

旧时模样 提交于 2019-12-11 13:59:31

问题


Dear people at Stackoverflow,

I would like to replace for every incoming e-mail the paragraph marks (^p i believe, at least in outlook Find & Replace) with manual line breaks (^l).
I've not been able to find a solution trough Google, but I might be searching wrong.

I am using the following code as a rule for every incoming e-mail:
(FYI this code works just fine with text)

Sub testing(MyMail As MailItem)
 MyMail.HTMLBody = Replace(MyMail.HTMLBody, "example", "changedtext")
 MyMail.Save
End Sub

Now I have tried to change the 2nd line to:

MyMail.HTMLBody = Replace(MyMail.HTMLBody, "^p", "^l")

And

MyMail.HTMLBody = Replace(MyMail.HTMLBody, "chr(13)", "chr(10)")

But these did not seem to work.

Unfortunately I'm not very familiar with VBA coding.
I've just been told I need to use chr() but I don't have a clue on how to do that.


Some background information:
I am using 2 rules, 1 to change every ^p with ^l and the other rule is to convert the email from HTML to plain text.
If I just convert it without first changing the ^p with ^l it will have all these extra empty lines.

Example:

Is someone out there that is willing to help me with this?
I would really appreciate it!

Regards,

Kris


回答1:


There are several ways to do it, but all with the same side-effect. If user SHIFT + ENTER 2 times, they will result in 1 new line also

Solution 1: 'Replace 2 newline into 1 newline

'vbCrLf is actually Chr(13) & Chr(10)

mail.HTMLBody = Replace(mail.HTMLBody, vbCrLf & vbCrLf, vbCrLf)

Solution 2: 'Replace any extra newline into "" there will be an extra blank link at the very end

tmp = Split(mail.HTMLBody, vbCrLf)
For Each Line In tmp
    If Line <> "" Then
        newBody = newBody  & Line & vbCrLf
    End If
Next
mail.HTMLBody = newBody


来源:https://stackoverflow.com/questions/13950252/vba-outlook-replace-paragraph-mark-with-manual-line-break

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!