Nathan
Nathan

Reputation: 23

How to replace sentences between 2 key words in an email?

I had earlier asked a question at this link on regex How to delete certain sentences between 2 words before forwarding the email?

I went back and tried the suggestion and made some observations. The below is my script where I am trying to:

  1. Change the email subject
  2. Insert a new line to the email body
  3. Locate and replace the sentences in the email body between the words "Impact" and "Corrective action"
  4. Forward the email
Set FwdMsg = item.Forward
With FwdMsg

Dim regEx As New RegExp
   With regEx
     .Global = False
     .multiline = True
     .ignorecase = False
     .pattern = strPattern
    End With

    Dim newbody As String
    Dim source As String
    source = FwdMsg.HTMLBody

    Dim replacestr As String
    replacestr = "$1\n\nplease call me with this number\n\n$2"
    strPattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
    newbody = regEx.replace(source, replacestr)
    FwdMsg.HTMLBody = newbody

    NewLine = "Dear users,Please note that data is also affected by the incident below and will be corrected. Please email  for more information."

    FwdMsg.HTMLBody = NewLine & FwdMsg.HTMLBody 
    FwdMsg.Recipients.Add "xx.com.sg"
    FwdMsg.Subject = "Incident" & Format$(Now, " dd-mm-yyyy hh.mmam/pm")

Somehow, I noticed a few things when I wrote my script on my Outlook.

  1. The code is unable to locate the sentences between the words Impact and Correction Action and hence those sentences are not removed.
  2. The replacestr line gets displayed on the email but not replacing those sentences between the words Impact and Correction Action.

Any ideas?

Upvotes: 2

Views: 107

Answers (2)

Looks to me like you didn't initialize strPattern before using it:

With regEx
 .Global = False
 .multiline = True
 .ignorecase = False
 .pattern = strPattern ' empty string ""
End With

At this point, nothing has been assigned to strPattern, so it contains nothing but an empty string "". So your regex is literally looking for the first occurrence of "", which I presume it finds right at the start of your e-mail. This is obviously not what you want.

To fix this, move the line in which you assign a value to strPattern up so it appears before the place where you use that variable, e.g.

strPattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
With regEx
 .Global = False
 .multiline = True
 .ignorecase = False
 .pattern = strPattern ' now it contains what you're looking for.
End With

Or, just get rid of that useless temp variable altogether! I don't see you using it anywhere else, so why not just inline it.

With regEx
 .Global = False
 .multiline = True
 .ignorecase = False
 .pattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
End With

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26687

How about

(Impact:\n)[\s\w]*(\nCorrective\s*Action)

changing the script as

replacestr = "$1\n\nplease call me with this number\n\n$2"
strPattern = "(Impact:\n)[\s\w]*(\nCorrective\s*Action)"

will produce output as

Impact:

please call me with this number

Corrective Actio

see the example on regex http://regex101.com/r/gU3aS1/2

Upvotes: 1

Related Questions