我认为这将是一个简单的宏,但我发现不可能完全实现您的要求;但是,我已经取得了一些接近。我没有删除我的诊断代码,所以你可以自己试验一下,也许会发现一系列我没有尝试过的语句。
这是进行更改的宏:
Public Sub ReduceBody(ItemCrnt As Outlook.MailItem)
Dim ReducedBody As String
With ItemCrnt
' Not all items in Inbox are mail items. It should not be possible for
‘ a non-mail-item to reach this macro but check just in case.
If .Class = olMail Then
' I test for a particular subject and a particular sender
' Many properties of a mail item can be checked in this way. Adjust
' the If statement as necessary
If LCase(.Subject) = "attachments" And _
LCase(.SenderEmailAddress) = "xxxxx.com" Then
Debug.Print "Html: [" & Replace(Replace(.HtmlBody, vbLf, "{l}"), vbCr, "{r}") & "]"
Debug.Print "Text: [" & Replace(Replace(.Body, vbLf, "{l}"), vbCr, "{r}") & "]"
Debug.Print "Format: " & .BodyFormat
Debug.Assert False ' Have a look at the initial values of the properties
' Save reduced body because clearing the Html body also clears the text body
ReducedBody = Left$(.Body, 20)
.BodyFormat = olFormatPlain ' Set body format to plain text
.HtmlBody = "<BODY>" & ReducedBody & "</BODY>"
Debug.Print "Html: [" & .HtmlBody & "]"
Debug.Print "Text: [" & .Body & "]"
Debug.Print "Format: " & .BodyFormat
Debug.Assert False ' Have a look at the new values of the properties
.Close (olDiscard) ' Delete when the new
Exit Sub ‘ values are as you require
.Save ' Save amended mail item
End If
End If
End With
End Sub
我相信我的 cmets 充分解释了宏的结构。
一旦宏确认它传递的项目是它应该处理的项目,它会将 Html 正文、文本正文和正文格式的当前值输出到即时窗口并使用Debug.Assert 停止处理.当您准备好继续时,请单击 F5。
代码修改这三个属性,显示它们的新值并再次停止。
我早就知道 Outlook 会从 Html 正文构建文本正文,但我没有意识到 Html 正文、文本正文和正文格式之间的联系。改变其中任何一个都会改变其他的。我提供的修改代码是我能够创建的最好的代码:
- 正文 = 原始正文的前 20 个字符
- Html body = “” & 原文正文的前 20 个字符 & “”
- 正文格式 = Html
当您使用 F5 重新启动宏时,更改将被丢弃。除非放弃更改,否则即使您不执行 save 命令,它们也会被保存。保留丢弃语句,直到显示的值可以接受。
为了测试上面的宏,我使用了:
Sub TestReduceBody()
Dim Exp As Explorer
Dim ItemCrnt As MailItem
Set Exp = Outlook.Application.ActiveExplorer
If Exp.Selection.Count = 0 Then
Call MsgBox("Please select one or more emails then try again", vbOKOnly)
Exit Sub
Else
For Each ItemCrnt In Exp.Selection
Call ReduceBody(ItemCrnt)
Next
End If
End Sub
我使用这样的宏来测试我所有的新邮件,处理宏。选择一个或多个邮件项目,然后启动此宏。这个宏允许我从一个简单的电子邮件开始,只有当它被正确处理时,我才能尝试更复杂的电子邮件。我有几个电子邮件地址,并且我从辅助帐户向我的主帐户发送了合适的测试电子邮件。您将准备好测试的真实电子邮件。我强烈推荐使用这样的宏。
根据您的要求修改第一个宏后,设置规则并将规则链接到此宏。我假设您知道如何创建规则,但如有必要,我可以提供说明。