如何删除Outlook会话(员工)并在VBA 6中保留原始邮件(客户)以及最后一个回复?

时间:2018-03-12 17:31:02

标签: vba visual-studio outlook outlook-addin outlook-vba

如何在VBA 6 for Outlook中创建一个删除所有会话(员工)的程序,并将原始邮件保留在我的最后回复中?

请参阅附件:

Image

我尝试将消息放在侧文件文本中,但之后我不知道如何在文件内部搜索以删除对话,所以我希望能帮助我做到这一点,我现在就把我的代码放进去了

    Sub remove_Forward(Item As Outlook.MailItem)
Dim oReply As MailItem
With Item
    Set oReply = .Forward
    Dim MyValue As Integer
      Dim reciverEmail As String
      Dim FileToString As String
                  Dim intFile As Integer

      Dim sFileText As String
Dim iFileNo As Integer
  iFileNo = FreeFile
      'open the file for reading
  Open "C:\Users\LKLK3\Desktop\test2.txt" For Input As #iFileNo
'change this filename to an existing file!  (or run the example below first)

      'read the file until we reach the end
  Do While Not EOF(iFileNo)
    Input #iFileNo, sFileText
      'show the text (you will probably want to replace this line as appropriate to your program!)
      Loop

      'close the file (if you dont do this, you wont be able to open it again!)
  Close #iFileNo




          reciverEmail = .Body


intFile = FreeFile

Open "C:\Users\LKLK3\Desktop\test2.txt" For Output As #intFile

Print #intFile, reciverEmail
Close #intFile


    Set oReply = Item

End With

End Sub

1 个答案:

答案 0 :(得分:0)

你的目标很难但我相信不可能。这个答案不是解决方案,而是对如何实现目标的解释。

看一下这个答案:How to copy Outlook mail message into excel using VBA or Macros。提问者不明白简单电子邮件背后的Html有多复杂,答案中的宏旨在帮助理解。我相信你有同样的问题。

宏创建一个新的Excel工作簿,并将收件箱中每个电子邮件的选定属性输出到该工作簿中的工作表。特别是,它将Html主体输出为字符串,以便您可以看到VBA宏的外观。宏用[CR]和[LF]替换回车符和换行符等控制字符,使它们可见而不是服从。将此宏复制到Outlook安装并按照修改说明进行操作,以便它在您的系统上运行。运行它并查看您要编辑的电子邮件的Html正文。

为了让您了解您将看到的内容,我已经运行了宏,并在下面显示了我的一封电子邮件的输出。我用“xxx”替换了每个文本块,并用他们的首字母替换了人们的姓名和电子邮件地址。电子邮件的中间部分如下:

Part of sample email

在您的示例中,链中的每封电子邮件都朝向:“从......发送...到......主题......”。我的示例中的一些电子邮件是:“在日期时间 xxx写道”。每个电子邮件包都有自己的标题样式。您必须在要处理的电子邮件中识别所有不同的标题。

通过多手牌传递的电子邮件可能会显示为:

Latest text
Header 3
Text 3
Header 2
Text 2
Header 1
Original text

您想要保留“最新文字”。 “标题1”和“原始文本”并丢弃“标题3”,“文本3”,“标题2”和“文本2”。

查找“From:...”或“On Date ...”将是查找标题但不是最后一步的第一步。以下是我的示例电子邮件中的一些标题:

<div>[CR][LF] On Friday, 12 January 2018, 11:05:08 GMT, L &lt;L@gmail.com&gt; wrote:[CR][LF] </div>

<div><br clear="none">On 11 Jan 2018, at 23:44, L &lt;<a shape="rect" href="mailto:L@gmail.com" rel="nofollow" target="_blank">L@gmail.com</a>&gt; wrote:<br clear="none"><br clear="none"></div>

<div dir="ltr">On Thu, 11 Jan 2018, 11:02 J, &lt;<a shape="rect" href="mailto:J@btinternet.com" rel="nofollow" target="_blank">J@btinternet.com</a>&gt; wrote:<br clear="none"></div>

<div lang="EN-GB"><div class="ydpaff2140ayiv2245505134m_-5691341948790493700WordSection1"><div><div style="border-color: rgb(225, 225, 225) currentcolor currentcolor; border-style: solid none none; border-width: 1pt medium medium; padding: 3pt 0cm 0cm;"><p class="ydpaff2140ayiv2245505134MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> G [mailto:<a shape="rect" href="mailto:G@yahoo.co.uk" rel="nofollow" target="_blank">G@yahoo.co.uk</a>] <br clear="none"><b>Sent:</b> 11 January 2018 10:27<br clear="none"><b>To:</b> J &lt;<a shape="rect" href="mailto:J@btinternet.com" rel="nofollow" target="_blank">J@btinternet.com</a>&gt;; T &lt;<a shape="rect" href="mailto:T@virginmedia.com" rel="nofollow" target="_blank">T@virginmedia.com</a>&gt;; C &lt;<a shape="rect" href="mailto:C@gmail.com" rel="nofollow" target="_blank">C@gmail.com</a>&gt;; E&lt;<a shape="rect" href="mailto:E@gmail.com" rel="nofollow" target="_blank">E@gmail.com</a>&gt;; L &lt;<a shape="rect" href="mailto:L@gmail.com" rel="nofollow" target="_blank">L@gmail.com</a>&gt;; M &lt;<a shape="rect" href="mailto:M@gmail.com" rel="nofollow" target="_blank">M@gmail.com</a>&gt;; N &lt;<a shape="rect" href="mailto:N@gmail.com" rel="nofollow" target="_blank">N@gmail.com</a>&gt;<br clear="none"><b>Subject:</b> xxx<u></u><u></u></span></p></div></div></div></div>

注意1:即使两个标题来自同一个人,它们也不一定相同。

注意2:我在标题文本开头之前回到<div>,在标题文本结尾之后回到</div>

通知2是重要的一个。我近年来检查过的所有电子邮件和网页都使用这种方法。字符串<div>xxx</div>表示xxx将显示为块。 <div>中的属性指定了块的显示方式。每个<div>都有一个匹配的<\div>,您必须删除一个完整的块;这是从<div>到匹配的<\div>。离开迷路<div><\div>的效果未定义,可能会产生奇怪的结果。

如果你查看下面的完整电子邮件,你会发现:

<blockquote class="ydpaff2140ayiv2245505134gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"></blockquote>

这是另一种类型的阻止,用于指定文本左侧的一行。我的示例电子邮件中有几个<blockquote><\blockquote块。由于它们会阻止您希望保留的文本和要放弃的文本,因此您必须删除所有这些标记,但保留其内容。

我试图解释你必须解决的问题。我会手动编辑电子邮件,看看删除不同部分的效果。在尝试编写任何VBA代码之前,我会尝试直到获得我想要的效果。

以下是我的示例电子邮件。将整个电子邮件保存到名称为“Test Email.Html”的文件中。使用浏览器显示此文件。或者,您可以忽略我的示例电子邮件并专注于您自己。

<html><head></head><body><div style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div>Hi all</div><div><br></div><div>xxx</div><div><br></div><div>xxx</div><div><br></div><div>G<br></div>[CR][LF] <div><br></div><div><br></div>[CR][LF] [CR][LF] <div id="ydpaff2140ayahoo_quoted_6195487581" class="ydpaff2140ayahoo_quoted">[CR][LF] <div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">[CR][LF] [CR][LF] <div>[CR][LF] On Friday, 12 January 2018, 11:05:08 GMT, L &lt;L@gmail.com&gt; wrote:[CR][LF] </div>[CR][LF] <div><br></div>[CR][LF] <div><br></div>[CR][LF] <div><div id="ydpaff2140ayiv2245505134"><div>xxx<div><br clear="none"></div><div>xxx</div><div><br clear="none"></div><div>xxx</div><div><br clear="none"></div><div>Lx<br clear="none"><br clear="none"><div id="ydpaff2140ayiv2245505134AppleMailSignature">Sent from my iPhone</div><div class="ydpaff2140ayiv2245505134yqt9119261735" id="ydpaff2140ayiv2245505134yqt17968"><div><br clear="none">On 11 Jan 2018, at 23:44, L &lt;<a shape="rect" href="mailto:L@gmail.com" rel="nofollow" target="_blank">L@gmail.com</a>&gt; wrote:<br clear="none"><br clear="none"></div><blockquote type="cite"><div></div></blockquote></div></div></div><div class="ydpaff2140ayiv2245505134yqt9119261735" id="ydpaff2140ayiv2245505134yqt79627"><div>xxx<div><br clear="none"></div><div>Lx<br clear="none"><br clear="none"><div id="ydpaff2140ayiv2245505134AppleMailSignature">Sent from my iPhone</div><div><br clear="none">On 11 Jan 2018, at 20:22, C &lt;<a shape="rect" href="mailto:C@gmail.com" rel="nofollow" target="_blank">cjdallimore@gmail.com</a>&gt; wrote:<br clear="none"><br clear="none"></div><blockquote type="cite"><div><p dir="ltr">xxx </p>[CR][LF] <p dir="ltr">xxx</p>[CR][LF] <br clear="none"><div class="ydpaff2140ayiv2245505134gmail_quote"><div dir="ltr">On Thu, 11 Jan 2018, 11:02 J, &lt;<a shape="rect" href="mailto:J@btinternet.com" rel="nofollow" target="_blank">J@btinternet.com</a>&gt; wrote:<br clear="none"></div><blockquote class="ydpaff2140ayiv2245505134gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div lang="EN-GB"><div class="ydpaff2140ayiv2245505134m_-5691341948790493700WordSection1"><p class="ydpaff2140ayiv2245505134MsoNormal"><span>xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span>xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span>J<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> G [mailto:<a shape="rect" href="mailto:G@yahoo.co.uk" rel="nofollow" target="_blank">G@yahoo.co.uk</a>] <br clear="none"><b>Sent:</b> 11 January 2018 10:27<br clear="none"><b>To:</b> J &lt;<a shape="rect" href="mailto:J@btinternet.com" rel="nofollow" target="_blank">J@btinternet.com</a>&gt;; T &lt;<a shape="rect" href="mailto:T@virginmedia.com" rel="nofollow" target="_blank">T@virginmedia.com</a>&gt;; C &lt;<a shape="rect" href="mailto:C@gmail.com" rel="nofollow" target="_blank">C@gmail.com</a>&gt;; E &lt;<a shape="rect" href="mailto:E@gmail.com" rel="nofollow" target="_blank">E@gmail.com</a>&gt;; L &lt;<a shape="rect" href="mailto:L@gmail.com" rel="nofollow" target="_blank">L@gmail.com</a>&gt;; M &lt;<a shape="rect" href="mailto:M@gmail.com" rel="nofollow" target="_blank">M@gmail.com</a>&gt;; N&lt;<a shape="rect" href="mailto:N@gmail.com" rel="nofollow" target="_blank">N@gmail.com</a>&gt;<br clear="none"><b>Subject:</b> xxx<u></u><u></u></span></p></div></div><div lang="EN-GB"><div class="ydpaff2140ayiv2245505134m_-5691341948790493700WordSection1"><p class="ydpaff2140ayiv2245505134MsoNormal"><u></u>&nbsp;<u></u></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<a shape="rect" href="http://www.rightmove.co.uk/property-for-sale/property-123.html?premiumA=true" rel="nofollow" target="_blank">http://www.rightmove.co.uk/property-for-sale/property-123.html?premiumA=true</a><u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">G<u></u><u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><span><u></u>&nbsp;<u></u></span></p><p class="ydpaff2140ayiv2245505134MsoNormal"><a shape="rect" name="m_-5691341948790493700__MailEndCompose"><span><u></u>&nbsp;<u></u></span></a></p><span></span></div></div><div lang="EN-GB"><div class="ydpaff2140ayiv2245505134m_-5691341948790493700WordSection1"><div><div style="border-color: rgb(225, 225, 225) currentcolor currentcolor; border-style: solid none none; border-width: 1pt medium medium; padding: 3pt 0cm 0cm;"><p class="ydpaff2140ayiv2245505134MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> G [mailto:<a shape="rect" href="mailto:G@yahoo.co.uk" rel="nofollow" target="_blank">G@yahoo.co.uk</a>] <br clear="none"><b>Sent:</b> 11 January 2018 10:27<br clear="none"><b>To:</b> J &lt;<a shape="rect" href="mailto:J@btinternet.com" rel="nofollow" target="_blank">J@btinternet.com</a>&gt;; T &lt;<a shape="rect" href="mailto:T@virginmedia.com" rel="nofollow" target="_blank">T@virginmedia.com</a>&gt;; C &lt;<a shape="rect" href="mailto:C@gmail.com" rel="nofollow" target="_blank">C@gmail.com</a>&gt;; E&lt;<a shape="rect" href="mailto:E@gmail.com" rel="nofollow" target="_blank">E@gmail.com</a>&gt;; L &lt;<a shape="rect" href="mailto:L@gmail.com" rel="nofollow" target="_blank">L@gmail.com</a>&gt;; M &lt;<a shape="rect" href="mailto:M@gmail.com" rel="nofollow" target="_blank">M@gmail.com</a>&gt;; N &lt;<a shape="rect" href="mailto:N@gmail.com" rel="nofollow" target="_blank">N@gmail.com</a>&gt;<br clear="none"><b>Subject:</b> xxx<u></u><u></u></span></p></div></div></div></div><div lang="EN-GB"><div class="ydpaff2140ayiv2245505134m_-5691341948790493700WordSection1"><p class="ydpaff2140ayiv2245505134MsoNormal"><u></u>&nbsp;<u></u></p><div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<a shape="rect" href="http://www.rightmove.co.uk/property-for-sale/property-123.html?premiumA=true" rel="nofollow" target="_blank">http://www.rightmove.co.uk/property-for-sale/property-123.html?premiumA=true</a><u></u><u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">xxx<u></u><u></u></span></p></div><div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;"><u></u>&nbsp;<u></u></span></p></div><p class="ydpaff2140ayiv2245505134MsoNormal"><span style="font-size:10.0pt;">G<u></u><u></u></span></p></div></div></div></div></blockquote></div>[CR][LF] </div></blockquote></div></div></div></div></div>[CR][LF] </div>[CR][LF] </div></div></body></html>