How do I create a script to remove the body of incoming emails?VBA script for outlook to automatically open URLs from message body in a web browser, for all incoming mailsrule based email handling in outlook using scriptsAdd text to body of email based on recipient specifiedIntegrate Outlook “Run as Script” rule into Excel VBA code that sends emailOtlook vba and rule to forward email message and change subjectExtracting a word from Outlook Email Body while replying email with predefined TemplateExcel VBA to include body in the forwarded outlook emailVBA - Outlook script focusing on wrong emailHow can I build a VBA script that takes an incoming email, modifies its body and forwards it?How do I remove text in the body of an email before send?

Time at 1 g acceleration to travel 100 000 light years

Teferi's Time Twist and Gideon's Sacrifice

Explain why a line can never intersect a plane in exactly two points.

What is the most suitable position for a bishop here?

Synaptic Static - when to roll the d6?

"What is the maximum that Player 1 can win?"

Justifying Affordable Bespoke Spaceships

What was the flower of Empress Taytu?

Traversing Latin America & Caribbean: A Cryptic Journey

Subtract the Folded Matrix

What are the pros and cons for the two possible "gear directions" when parking the car on a hill?

Is it possible to transpose samples (in cents) from minor to major?

Why don't countries like Japan just print more money?

Rejecting an offer after accepting it just 10 days from date of joining

Print one file per line using echo

Dates on degrees don’t make sense – will people care?

Why do you need to heat the pan before heating the olive oil?

How much steel armor can you wear and still be able to swim?

Definition of 'vrit'

What type of tests you should do first?

Is declining an undergraduate award which causes me discomfort appropriate?

I just entered the USA without passport control at Atlanta airport

"Correct me if I'm wrong"

Are there any individual aliens that have gained superpowers in the Marvel universe?



How do I create a script to remove the body of incoming emails?


VBA script for outlook to automatically open URLs from message body in a web browser, for all incoming mailsrule based email handling in outlook using scriptsAdd text to body of email based on recipient specifiedIntegrate Outlook “Run as Script” rule into Excel VBA code that sends emailOtlook vba and rule to forward email message and change subjectExtracting a word from Outlook Email Body while replying email with predefined TemplateExcel VBA to include body in the forwarded outlook emailVBA - Outlook script focusing on wrong emailHow can I build a VBA script that takes an incoming email, modifies its body and forwards it?How do I remove text in the body of an email before send?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to create a script that I can use with a message rule, to remove the body of incoming emails. Ideally I would like to leave the first 20 characters intact and delete the rest of the email, but I would settle for deleting the entire contents.










share|improve this question
























  • This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

    – Tony Dallimore
    Mar 25 at 14:57











  • For further clarification...

    – Kelly Plekker
    Mar 26 at 22:14











  • For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

    – Kelly Plekker
    Mar 26 at 23:03











  • Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

    – Tony Dallimore
    Mar 27 at 0:42











  • I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

    – Tony Dallimore
    Mar 27 at 0:45

















0















I am trying to create a script that I can use with a message rule, to remove the body of incoming emails. Ideally I would like to leave the first 20 characters intact and delete the rest of the email, but I would settle for deleting the entire contents.










share|improve this question
























  • This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

    – Tony Dallimore
    Mar 25 at 14:57











  • For further clarification...

    – Kelly Plekker
    Mar 26 at 22:14











  • For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

    – Kelly Plekker
    Mar 26 at 23:03











  • Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

    – Tony Dallimore
    Mar 27 at 0:42











  • I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

    – Tony Dallimore
    Mar 27 at 0:45













0












0








0








I am trying to create a script that I can use with a message rule, to remove the body of incoming emails. Ideally I would like to leave the first 20 characters intact and delete the rest of the email, but I would settle for deleting the entire contents.










share|improve this question
















I am trying to create a script that I can use with a message rule, to remove the body of incoming emails. Ideally I would like to leave the first 20 characters intact and delete the rest of the email, but I would settle for deleting the entire contents.







vba outlook outlook-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 7:10









0m3r

8,31692555




8,31692555










asked Mar 25 at 6:53









Kelly PlekkerKelly Plekker

1




1












  • This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

    – Tony Dallimore
    Mar 25 at 14:57











  • For further clarification...

    – Kelly Plekker
    Mar 26 at 22:14











  • For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

    – Kelly Plekker
    Mar 26 at 23:03











  • Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

    – Tony Dallimore
    Mar 27 at 0:42











  • I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

    – Tony Dallimore
    Mar 27 at 0:45

















  • This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

    – Tony Dallimore
    Mar 25 at 14:57











  • For further clarification...

    – Kelly Plekker
    Mar 26 at 22:14











  • For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

    – Kelly Plekker
    Mar 26 at 23:03











  • Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

    – Tony Dallimore
    Mar 27 at 0:42











  • I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

    – Tony Dallimore
    Mar 27 at 0:45
















This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

– Tony Dallimore
Mar 25 at 14:57





This is a very strange request. Do you mean the body of every email or only some? What is the value of knowing who has sent you an email but not what they wrote?

– Tony Dallimore
Mar 25 at 14:57













For further clarification...

– Kelly Plekker
Mar 26 at 22:14





For further clarification...

– Kelly Plekker
Mar 26 at 22:14













For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

– Kelly Plekker
Mar 26 at 23:03





For further clarification...the email account I need this on, receives attachments from another sender, which is automatically pulled to another application. The process fails when there is more than 20 characters in the email body. The issue is the senders signature. I need it deleted so the extraction can happen. At this stage that involves manually deleting it.

– Kelly Plekker
Mar 26 at 23:03













Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

– Tony Dallimore
Mar 27 at 0:42





Is the problem with every email from this sender or only some? If only some, can you identify the emails that need their body's deleted? I think the easiest approach would be a rule that identified the sender and then an a script that deleted the Html body, and reduced the text body to 20 characters.

– Tony Dallimore
Mar 27 at 0:42













I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

– Tony Dallimore
Mar 27 at 0:45





I find it difficult to understand how a process that saves attachments can be affected by the size of the body. I assume this question is because the faulty process is not under your control and cannot easily be fixed.

– Tony Dallimore
Mar 27 at 0:45












1 Answer
1






active

oldest

votes


















0














I thought this would be simple macro but I have found it impossible to achieve exactly what you requested; however, I have achieved something close. I have not deleted my diagnostic code so you can experiment yourself and perhaps discover a sequence of statements that I have not tried.



This is the macro that makes the changes:



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


I believe my comments explain the structure of the macro adequately.



Once the macro has confirmed that the item it has been passed is one it should process, it outputs the current values of the Html body, the text body and the body format to the Immediate Window and uses Debug.Assert to stop processing. Click F5 when you are ready to continue.



The code modifies these three properties, displays their new values and stops again.



I have known for a long time that Outlook will build a text body from an Html body but I had not realised how linked the Html body, the text body and the body format are. Changing any of them changes the others. The modification code I have provided, is the best I have been able to create which is:



  • Text body = first 20 characters of original text body

  • Html body = “” & first 20 characters of original text body & “”

  • Body format = Html

When you restart the macro with F5, the changes will be discarded. Unless the changes are discarded, they will be saved even if you do not execute the save command. Keep the discard statements until the values displayed are acceptable.



To test the above macro, I used:



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


I use a macro like this to test all my new mail-item, processing macros. Select one or more mail items and then start this macro. This macro allows me to start with a simple email and, only when that is processed correctly, do I try more complicated emails. I have several email addresses and I sent suitable test emails from a secondary account to my main account. You will have genuine emails ready to test. I highly recommend using macros like this.



Once you have amended the first macro to your requirements, set up a rule and link the rule to this macro. I assume you know how to create a rule, but I can provide instructions if necessary.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332570%2fhow-do-i-create-a-script-to-remove-the-body-of-incoming-emails%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I thought this would be simple macro but I have found it impossible to achieve exactly what you requested; however, I have achieved something close. I have not deleted my diagnostic code so you can experiment yourself and perhaps discover a sequence of statements that I have not tried.



    This is the macro that makes the changes:



    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


    I believe my comments explain the structure of the macro adequately.



    Once the macro has confirmed that the item it has been passed is one it should process, it outputs the current values of the Html body, the text body and the body format to the Immediate Window and uses Debug.Assert to stop processing. Click F5 when you are ready to continue.



    The code modifies these three properties, displays their new values and stops again.



    I have known for a long time that Outlook will build a text body from an Html body but I had not realised how linked the Html body, the text body and the body format are. Changing any of them changes the others. The modification code I have provided, is the best I have been able to create which is:



    • Text body = first 20 characters of original text body

    • Html body = “” & first 20 characters of original text body & “”

    • Body format = Html

    When you restart the macro with F5, the changes will be discarded. Unless the changes are discarded, they will be saved even if you do not execute the save command. Keep the discard statements until the values displayed are acceptable.



    To test the above macro, I used:



    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


    I use a macro like this to test all my new mail-item, processing macros. Select one or more mail items and then start this macro. This macro allows me to start with a simple email and, only when that is processed correctly, do I try more complicated emails. I have several email addresses and I sent suitable test emails from a secondary account to my main account. You will have genuine emails ready to test. I highly recommend using macros like this.



    Once you have amended the first macro to your requirements, set up a rule and link the rule to this macro. I assume you know how to create a rule, but I can provide instructions if necessary.






    share|improve this answer



























      0














      I thought this would be simple macro but I have found it impossible to achieve exactly what you requested; however, I have achieved something close. I have not deleted my diagnostic code so you can experiment yourself and perhaps discover a sequence of statements that I have not tried.



      This is the macro that makes the changes:



      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


      I believe my comments explain the structure of the macro adequately.



      Once the macro has confirmed that the item it has been passed is one it should process, it outputs the current values of the Html body, the text body and the body format to the Immediate Window and uses Debug.Assert to stop processing. Click F5 when you are ready to continue.



      The code modifies these three properties, displays their new values and stops again.



      I have known for a long time that Outlook will build a text body from an Html body but I had not realised how linked the Html body, the text body and the body format are. Changing any of them changes the others. The modification code I have provided, is the best I have been able to create which is:



      • Text body = first 20 characters of original text body

      • Html body = “” & first 20 characters of original text body & “”

      • Body format = Html

      When you restart the macro with F5, the changes will be discarded. Unless the changes are discarded, they will be saved even if you do not execute the save command. Keep the discard statements until the values displayed are acceptable.



      To test the above macro, I used:



      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


      I use a macro like this to test all my new mail-item, processing macros. Select one or more mail items and then start this macro. This macro allows me to start with a simple email and, only when that is processed correctly, do I try more complicated emails. I have several email addresses and I sent suitable test emails from a secondary account to my main account. You will have genuine emails ready to test. I highly recommend using macros like this.



      Once you have amended the first macro to your requirements, set up a rule and link the rule to this macro. I assume you know how to create a rule, but I can provide instructions if necessary.






      share|improve this answer

























        0












        0








        0







        I thought this would be simple macro but I have found it impossible to achieve exactly what you requested; however, I have achieved something close. I have not deleted my diagnostic code so you can experiment yourself and perhaps discover a sequence of statements that I have not tried.



        This is the macro that makes the changes:



        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


        I believe my comments explain the structure of the macro adequately.



        Once the macro has confirmed that the item it has been passed is one it should process, it outputs the current values of the Html body, the text body and the body format to the Immediate Window and uses Debug.Assert to stop processing. Click F5 when you are ready to continue.



        The code modifies these three properties, displays their new values and stops again.



        I have known for a long time that Outlook will build a text body from an Html body but I had not realised how linked the Html body, the text body and the body format are. Changing any of them changes the others. The modification code I have provided, is the best I have been able to create which is:



        • Text body = first 20 characters of original text body

        • Html body = “” & first 20 characters of original text body & “”

        • Body format = Html

        When you restart the macro with F5, the changes will be discarded. Unless the changes are discarded, they will be saved even if you do not execute the save command. Keep the discard statements until the values displayed are acceptable.



        To test the above macro, I used:



        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


        I use a macro like this to test all my new mail-item, processing macros. Select one or more mail items and then start this macro. This macro allows me to start with a simple email and, only when that is processed correctly, do I try more complicated emails. I have several email addresses and I sent suitable test emails from a secondary account to my main account. You will have genuine emails ready to test. I highly recommend using macros like this.



        Once you have amended the first macro to your requirements, set up a rule and link the rule to this macro. I assume you know how to create a rule, but I can provide instructions if necessary.






        share|improve this answer













        I thought this would be simple macro but I have found it impossible to achieve exactly what you requested; however, I have achieved something close. I have not deleted my diagnostic code so you can experiment yourself and perhaps discover a sequence of statements that I have not tried.



        This is the macro that makes the changes:



        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


        I believe my comments explain the structure of the macro adequately.



        Once the macro has confirmed that the item it has been passed is one it should process, it outputs the current values of the Html body, the text body and the body format to the Immediate Window and uses Debug.Assert to stop processing. Click F5 when you are ready to continue.



        The code modifies these three properties, displays their new values and stops again.



        I have known for a long time that Outlook will build a text body from an Html body but I had not realised how linked the Html body, the text body and the body format are. Changing any of them changes the others. The modification code I have provided, is the best I have been able to create which is:



        • Text body = first 20 characters of original text body

        • Html body = “” & first 20 characters of original text body & “”

        • Body format = Html

        When you restart the macro with F5, the changes will be discarded. Unless the changes are discarded, they will be saved even if you do not execute the save command. Keep the discard statements until the values displayed are acceptable.



        To test the above macro, I used:



        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


        I use a macro like this to test all my new mail-item, processing macros. Select one or more mail items and then start this macro. This macro allows me to start with a simple email and, only when that is processed correctly, do I try more complicated emails. I have several email addresses and I sent suitable test emails from a secondary account to my main account. You will have genuine emails ready to test. I highly recommend using macros like this.



        Once you have amended the first macro to your requirements, set up a rule and link the rule to this macro. I assume you know how to create a rule, but I can provide instructions if necessary.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 15:11









        Tony DallimoreTony Dallimore

        10.8k42252




        10.8k42252





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332570%2fhow-do-i-create-a-script-to-remove-the-body-of-incoming-emails%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해