Delete selected text from incoming emails

Status
Not open for further replies.

Dr. Bartolo

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
I am not sure I understand that.

If I write this:

If Item.BodyFormat = olFormatPlain Then

b = lcase(Item.Body)
OldText = Replace(Item.body, "Text to Remove" & vbCrLf, "")
NewText = ""

Item.body = b
End If

which is the code currently not working as it should, where would I add b = Item.body?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Correct - i had not considered this
b = lcase(Item.Body)
Item.body = b

you definitely need to use
b = Item.Body
Item.body = b

which means the "text to remove." need to be the same case as it is in the message. Test it with just text, no ending space or new line: "Text to remove." not "Text to remove. " or "Text to remove. " & vbcrlf

Also... others ways to reference breaks is with chr(10), chr(13), or vbNewLine

oh, and don't forget to save changes. Item.save
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Correct - i had not considered this
b = lcase(Item.Body)
Item.body = b

you definitely need to use
b = Item.Body
Item.body = b

which means the "text to remove." need to be the same case as it is in the message. Test it with just text, no ending space or new line: "Text to remove." not "Text to remove. " or "Text to remove. " & vbcrlf

Also... others ways to reference breaks is with chr(10), chr(13), or vbNewLine

This worked on my plain text message to remove extra lines at the top - i added it as 3rd replace in the plain text part.
OldText = vbNewLine & vbNewLine & "words in first line"
NewText = "words in first line"
b = Replace(b, OldText, NewText, , , vbTextCompare)


oh, and don't forget to save changes. Item.save
 

Dr. Bartolo

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
I find it hard to work out quite what changes you are advising me to make, so here is the full amended code - which produces exactly the same result - nothing changes!

If Item.BodyFormat = olFormatPlain Then

b = Item.body
OldText = "Text to remove."
NewText = ""
b = Replace(b, OldText, NewText, , , vbTextCompare)

Item.body = b
End If

Item.Save

Set Item = Nothing

I am sure I am missing something very simple, but for the life of me I cannot see what.
 

Dr. Bartolo

Member
Outlook version
Outlook 2016 32 bit
Email Account
POP3
The something simple was to close and restart Outlook!

Now it all seems to be working as it should. I shall keep an eye on it for the next few days to be sure.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Oh, yeah... you do often need to kick start the application start up macros for changes to take effect (especially after an error). I'm so used to doing it with every change, that i forget to mention it.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
R VBA Code to permanently delete selected email Outlook VBA and Custom Forms 10
mikolajek Random message selected after hard delete Using Outlook 4
Horsepower Cannot delete gmail in iPhone Outlook outbox Using Outlook 1
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
N How to add or delete items to Move dropdown Menu Using Outlook 0
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 1
O How to delete a forum post? Using Outlook 4
T Outlook 2010 Errore run-time -2147417851 (80010105) Metodo delete ContactItem non riuscito Outlook VBA and Custom Forms 0
C Outlook 2016 Unable to delete Gmail IMAP calendar Using Outlook 2
S Outlook 2016 dont delete inbox item Using Outlook 0
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
O Outlook 2010 Add delete button to the side of the message list Using Outlook 1
R Outlook 365 update sets delete from server flag Using Outlook 2
S Outlook 2016 A Shortcut to recall and delete and sent message. Using Outlook 1
T Outlook 2010 How to Delete “Orphaned” TO DO? ( Not on Exchange Server) Using Outlook 6
P How did hacker cause link to delete and archive containing email? Using Outlook 6
Z Outlook 365 delete reminder you can’t make change to contents of this-read only folder Using Outlook 4
J To delete the draft email Using Outlook 2
W Recurrence: delete older occurrences / change earliest start time Outlook VBA and Custom Forms 0
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
A Attachments.Delete Outlook VBA and Custom Forms 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
M Delete headers in Inline reply Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S How can I delete all copies of a given companies logo graphic from all my emails at once? Using Outlook 3
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1
D after delete mail, open the next one Outlook VBA and Custom Forms 0
O Delete duplicate emails - subscription notifications Using Outlook 5
F Delete/create/reset Exchange mailbox on Outlook.com Using Outlook.com accounts in Outlook 3
L Email Read and Delete Outlook VBA and Custom Forms 4
H Delete Calendar Entries in Using Outlook 2
D Delete Emails from Senders in Shared Mailbox Outlook VBA and Custom Forms 1
R How to delete Facebook Contacts folder in Outlook 365 Using Outlook 7
JoeG Appointment Delete/Change Recurrence Outlook VBA and Custom Forms 0
C Outlook 2016 cannot delete, email showing in root Exchange Server Administration 5
C How to replace or delete first instance of sentence in mail body? Outlook VBA and Custom Forms 1
Z Can't delete IMAP folder Using Outlook 2
J Delete old emails Exchange Server Administration 1
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
T Delete Emails from search results Outlook VBA and Custom Forms 1
I Outlook 2013 How do I delete a Calendar? Using Outlook.com accounts in Outlook 1
N How to delete default folder "Contacts" (olFolderContacts) in a .pst file Using Outlook 8
O How to delete posts Using Outlook 1
B How to 'really' delete IMAP emails? Using Outlook 6
H Search and delete part of a link inside HTML body message Outlook VBA and Custom Forms 2
B Delete/replace old files and save new attachments Using Outlook 1
Jeanne Goodman Delete Calendar Item Bypassing Deleted Items folder Outlook VBA and Custom Forms 2
C Delete Subject-Add Organizer to all Reservations in Default Calendar. Using Outlook 0
P Delete mail macro Outlook VBA and Custom Forms 6

Similar threads

Top