vba - Delete or overwrite body text AFTER a given keyword -


the purpose of search create script outlook, that:

  1. removes password field body of selected inbox email
  2. & saves hard-drive set folder administrative purposes.

so every email contains line "password: xyz" don't want delete constant part "password:", dynamic part after it. either deleting whole line after keyword, or overwriting dynamic part long enough string "pw removed copy" or similar.

i figure out how search given text in mail body, , how replace it, or how insert text mail-body, couldn't find anywhere, how modify (delete or over-wright) text after search term , not search term itself.

my demo code in stage right now: (only allowing replace known text, cannot reach unknown part it) (( can see code contains switched off lines ".insertbefore"; experimenting approach bit without success, turned off ))

public sub savemessageasmsg()     dim omail outlook.mailitem     dim objitem object     dim spath, strfolderpath string     dim dtdate date     dim sname string     dim enviro string     dim body string      enviro = cstr(environ("userprofile"))      spath = "d:\demo\"      each objitem in activeexplorer.selection         if objitem.messageclass = "ipm.note"             set omail = objitem             sname = omail.subject             body = omail.body             body = replace(body, "password:", "password: -removed-")             'objsel.insertbefore strtext             omail.body = body             'debug.print spath & sname             omail.saveas spath & sname & ".msg", olmsg          end if     next end sub 

ok guys found workaround question, can barely beleive there no simpler method this, if can come more elegant solution, pls (just learn right way)

basically used 2 searches find positions of first known word , next known word in text , substracted them eachother number of characters in between them , pasted string in between.

the benefit of method @ least assures wont hurt content of next line, no matter how long overwritten unknown text was.

the key element in solution mid statement.

public sub savemessagewithoutpw()     dim omail outlook.mailitem     dim objitem object     dim spath, strfolderpath string     dim sname string     dim enviro string     dim body string     dim testpos integer     dim endpos integer     dim posl integer      enviro = cstr(environ("userprofile"))      spath = "d:\demo\"      each objitem in activeexplorer.selection         if objitem.messageclass = "ipm.note"             set omail = objitem             sname = omail.subject             body = omail.body             testpos = instr(body, "password") + 9 '+9 password             endpos = instr(body, "send email")             posl = (endpos - testpos) - 1 '-1 linebreak             mid(body, testpos, posl) = " ******************************"             'posl defines how many * use @ most, not hurt other content             omail.body = body             omail.saveas spath & sname & ".msg", olmsg          end if     next end sub 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -