WildCard for deleting lines in an email message body.

RWayneH
RWayneH used Ask the Experts™
on
Use the following line to delete items from an email message .body.

myforward.Body = Replace(myforward.Body, "From: batchsd <batchsd@Haworth.com>", "")

Open in new window


Is there a way to add a wildcard to this?  So everything after From: is deleted too?  I have lines that are not static.  Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Try placing the following function on a Standard Module...

Function ReplacedBody(str As String) As String
With CreateObject("VBScript.RegExp")
   .Global = False
   .Pattern = "(.*)From:.*"
    If .test(str) Then
       ReplacedBody = .Replace(.Execute(str)(0), "$1")
    End If
End With
End Function

Open in new window

And then use this like below and see if that works for you...

myforward.Body = ReplacedBody(myforward.Body)

Open in new window

Author

Commented:
Thanks
Ah, this hosed the whole message body, I just want to remove the line that has the "Sent:" in it.  Just like:

myforward.Body = Replace(myforward.Body, "To: SAPDocuments <SAPDocuments@Haworth.com>", "")

Open in new window


only get rid of the: "To: SAPDocuments <SAPDocuments@Haworth.com>"

The actual text that I am trying to remove is: Sent: Monday, December 17, 2018 3:47 AM
As you can see anything after the Sent: is a date and is changes.  by getting rid of the line, perhaps wildcarding to the AM or PM?

"Sent: * AM"  or PM"
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Change the existing function to this........

Function ReplacedBody(str As String) As String
With CreateObject("VBScript.RegExp")
   .Global = False
   .Pattern = "Sent:.*(AM|PM)"
    If .test(str) Then
        ReplacedBody = Replace(str, .Execute(str)(0), "")
    End If
End With
End Function

Open in new window


myforward.Body = ReplacedBody(myforward.Body)

Open in new window

This will return everything before and after Sent: <DateStamp String>

Author

Commented:
Thanks for the help it worked!!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial