The purpose of my search is to create a script for Outlook, that:
- Removes a password field from the body of a selected inbox email
- & saves it to the hard-drive to a set folder for administrative
So basically every email contains a line like "password: xyz"
I don't want to delete the constant part "password:", only the dynamic part after it.
Either by deleting the whole line after the keyword, or by overwriting the dynamic part with a long enough string like "PW removed from this copy" or similar.
I could figure out how to search for a given text in mail body, and how to replace it, or how to insert text into a mail-body,
BUT i couldn't find anywhere, how to modify (delete or over-wright) text AFTER the search term and not the search term itself.
My demo code is in this stage right now: (only allowing to replace already known text, but cannot reach to the unknown part with it)
(( you can see that my code contains switched off lines like ".insertbefore"; I was experimenting with that approach a bit too just without success, so turned it off for now ))
Public Sub SaveMessageAsMsg()
Dim oMail As Outlook.MailItem
Dim objItem As Object
Dim sPath, strFolderpath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String
Dim body As String
enviro = CStr(Environ("USERPROFILE"))
sPath = "D:\Demo\"
For Each objItem In ActiveExplorer.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem
sName = oMail.Subject
body = oMail.body
body = Replace(body, "Password:", "Password: -Removed-")
oMail.body = body
'Debug.Print sPath & sName
oMail.SaveAs sPath & sName & ".msg", olMSG