基于VBA的自定义规则阻止接收传入的电子邮件或中断图形元素

时间:2014-05-20 07:05:53

标签: vba outlook-vba

我写了下面的规则,它应该在某些条件下(这里没有硬编码)接收包含重要图形元素的传入邮件,并通过.HTMLbody上的操作中断所有超链接,然后将该电子邮件发送给另一个人。< / p>

Sub customRuleHiperlinkRemove2(MyMail As MailItem)
   'this block receiving of incoming mail
    Set currentEmail = MyMail

    currentEmail.To = "anotherPerson@xxx.xx"
    currentEmail.Subject = "newSubject"
    currentEmail.HTMLBody = Replace(currentEmail.HTMLBody, " href=", "")
    currentEmail.Send
    Set currentEmail = Nothing

End Sub

但是上面的代码阻止了MyMail消息的接收。

下面的代码解决了这个问题,但另一方面打破了传入邮件中包含的所有图形元素:

Sub customRuleHiperlinkTest1(MyMail As MailItem)
   'this breaks all graphical elements (do not copy them to new mail)
   Set objOutlook = CreateObject("Outlook.Application")
   Set newEmail = objOutlook.CreateItem(0)

   newEmail.HTMLBody = MyMail.HTMLBody
   newEmail.HTMLBody = Replace(currentEmail.HTMLBody, " href=", "")

   newEmail.To = "anotherPerson@xxx.xx"
   newEmail.Subject = "newSubject"
   newEmail.Send
   Set newEmail = Nothing

End Sub

我可以通过对收到的消息进行前瞻,然后运行打破链接然后发送该消息的宏来实现这一点,但是是否有程序化的解决方案?

1 个答案:

答案 0 :(得分:1)

.Copy上使用MyMail方法解决了问题:

Sub customRuleHiperlinkRemove2(MyMail As MailItem)

   Set currentEmail = MyMail.Copy ' .Copy solves the problem

   currentEmail.To = "anotherPerson@xxx.xx"
   currentEmail.Subject = "newSubject"
   currentEmail.HTMLBody = Replace(currentEmail.HTMLBody, " href=", "")
   currentEmail.Send
   Set currentEmail = Nothing

End Sub