vba - Macro for saving emails from outlook - Sender name query -


so, organisation work requires all project or task related emails saved, part of formal process, in specific name format:

yymmdd-hhmm-sendername-recipientname-subject.msg

we're not allowed use .pst files reason can imagine, doing individually tedious in extreme, , lazy, decided try , automate helpful macro. intensive googling led me create following website called slipstick:

option explicit public sub savemessageasmsg()   dim omail outlook.mailitem   dim objitem object   dim spath string   dim dtdate date   dim sname string   dim enviro string   dim strfolderpath string       enviro = cstr(environ("userprofile")) strfolderpath = "desktop location here"     each objitem in activeexplorer.selection    if objitem.messageclass = "ipm.note"     set omail = objitem    sname = omail.subject   replacecharsforfilename sname, "-"    dtdate = omail.receivedtime   sname = format(dtdate, "yyyymmdd", vbusesystemdayofweek, _     vbusesystem) & format(dtdate, "-hhnn", _     vbusesystemdayofweek, vbusesystem) & "-" & sname & ".msg"    spath = strfolderpath & "\"   debug.print spath & sname   omail.saveas spath & sname, olmsg    end if   next  end sub  private sub replacecharsforfilename(sname string, _   schr string _ )   sname = replace(sname, "'", schr)   sname = replace(sname, "*", schr)   sname = replace(sname, "/", schr)   sname = replace(sname, "\", schr)   sname = replace(sname, ":", schr)   sname = replace(sname, "?", schr)   sname = replace(sname, chr(34), schr)   sname = replace(sname, "<", schr)   sname = replace(sname, ">", schr)   sname = replace(sname, "|", schr) end sub 

this quite happily saves selected emails in folder on desktop (for moving on elsewhere) in format

yyyymmdd-hhmm-subject.msg

try might, however, can't seem sender's name go in there well. i've tried using

dim ssendername string 

and

dim ssenderemailaddress string 

with appropiate additions in part of macro creates file name:

sname = format(dtdate, "yyyymmdd", vbusesystemdayofweek, _         vbusesystem) & format(dtdate, "-hhnn", _         vbusesystemdayofweek, vbusesystem) & "-" *& bit here* & "-" & sname & ".msg 

but seems add hyphen eventual filename.

note i'm not bothered adding recipient filename, more few recipeints send sailing past character limit.

so, doing wrong? i'm complete newcomer vba , have quite enjoyed little project, i'd love know i'm fouling up. in defence i'm mechanical engineer, , i'm pretty sure hitting macro hammer won't help.

i'm considering trying write produce little popup letting me fill in pertinent info before saving email, that's little way beyond me right now. gotta walk before can run.

all tips , comments gratefully received.

if using dim ssenderemailaddress string without ever setting why double hyphen - it's empty.

try including:

dim ssenderemailaddress string ssenderemailaddress = omail.senderemailaddress 

before create file name this:

sname = format(dtdate, "yyyymmdd", vbusesystemdayofweek, _         vbusesystem) & format(dtdate, "-hhnn", _         vbusesystemdayofweek, vbusesystem) & "-" & ssenderemailaddress & "-" & sname & ".msg 

you should able use sendername in manner if like, although you're best still run through function strip out characters.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -