-2

How can I make a program in VBA 6 for Outlook that delete all conversation (employees) in forward and just keep the original message with last reply from me?

See attachment please:

Image

i try to put the message in side file text but after that i don't know how to search inside file text to remove the conversation so please i hope to help me to do it i wail put my code right now

    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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    May we see your research/code so far, edited into the question? What are you stuck on specifically? – halfer Mar 12 '18 at 17:38
  • i try to put the message in side file text but after that i don't know how to search inside file text to remove the conversation so please i hope to help me to do it i wail put my code right now – انس الواصل Mar 12 '18 at 17:48
  • ***"Welcome to [so]!"*** You might have the wrong idea about this site; it's a place for programmers to give/get help when a solution to a problem can't be found elsewhere. The best questions include code samples, but any ***specific,*** practical, answerable programming problem is welcome . Please see the [tour] as well as "[help/on-topic]" and also "[ask]". Also there's good tips [here](https://codeblog.jonskeet.uk/writing-the-perfect-question) and "[mcve]". – ashleedawg Mar 12 '18 at 18:03
  • A mail item can have a text body or an Html body or a Rich Text Format (RTF) body. I have never seen a RTF body. Most emails I receive have both a text body and an Html body. If both are present, the Html body is the one shown to the user. You are looking at .Body which holds the text body. If you want to edit the Html body, you need to look at .HTMLBody. – Tony Dallimore Mar 12 '18 at 20:14
  • yes sr i wail do but the problems is how to search in side html file and delete the forward that is the question please can you help me to do that – انس الواصل Mar 14 '18 at 11:24

1 Answers1

0

Your objective will be difficult but not, I believe, impossible. This answer is not a solution but an explanation of how I would attempt to achieve your objective.

Have a look at this answer: How to copy Outlook mail message into excel using VBA or Macros. The questioner did not understand how complex the Html behind a simple email can be and the macro in the answer was intended to help give that understanding. I believe you have the same problem.

The macro creates a new Excel workbook and outputs selected properties of every email in Inbox to a worksheet within that workbook. In particular, it outputs the Html body as a string so you can see what it would look like to a VBA macro. The macro replaces control characters such as carriage return and linefeed with [CR] and [LF] so they are visible rather than obeyed. Copy this macro to your Outlook installation and follow the instructions for amending it so it runs on your system. Run it and look at the Html bodies of the emails you wish to edit.

To give you an idea of what you will see, I have run the macro and below I show the output for one of my emails. I have replaced each block of text with “xxx” and replaced people’s names and email addresses with their initials. The middle of that email looks like:

Part of sample email

In your example, each email in the chain is headed: “From … Sent … To … Subject …”. Some of the emails in my example are headed: “On date at time xxx wrote”. Each email package has its own style for these headers. You will have to identify all the different headers in the emails you wish to process.

An email that has been passed through several hands might display as:

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

You want to keep “Latest text”. “Header 1” and “Original text” and discard “Header 3”, “Text 3”, “Header 2” and “Text 2”.

Finding “From: …” or “On Date …” will be the first step in finding a header but not the last step. Below are some of the headers from my sample email:

<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>

Notice 1: even when two headers are from the same person, they are not necessarily the same.

Notice 2: I have gone back to the <div> before the start of the header’s text and the </div> after the end of the header’s text.

Notice 2 is the important one. All emails and web pages that I have examined in recent years use this approach. The string <div>xxx</div> says xxx is to be displayed as a block. The attributes within <div>specify how the block is to be displayed. Each <div> has a matching <\div> and you must remove a complete block; that is from <div> to its matching <\div>. The effect of leaving a stray <div> or <\div> is not defined and may give strange results.

If you look at the full email below you will find:

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

This is another type of blocking which has been used to specify a line down the left side of the text. There are several <blockquote> to <\blockquote blocks within my example email. Since they block together text you wish to keep and text you wish to discard, you will have to remove all these tags but retain their content.

I have tried to explain the issues you will have to address. I would manually edit emails and see the effect of deleting different parts. I would experiment until I got the effect I wanted before trying to code any VBA.

Below is my sample email. Save the entire email from to to a file with a name such as “Test Email.Html”. Display this file with your browser. Alternatively, you could ignore my example email and concentrate on your own.

<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>

Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • Thank you very much for your help. I will never forget this But I had a problem when running the program The Excel file is frozen as this image has tried a lot to fix the problem but without benefit I hope you can help me fix this problem this is link of image https://b.top4top.net/p_825jpchm1.jpg – انس الواصل Apr 05 '18 at 16:28
  • @انسالواصل You do not say what code freezes. You do not say where it freezes. I cannot tell what is wrong from an image. – Tony Dallimore Apr 09 '18 at 11:16