vba - how to save excel file attached in an email received in a defined sub folder in the inbox of outlook 2007 to a folder on the windows? -
i need save excel attachement received inside outlook messages in specific sub folder (daily final) located in inbox , knowing emails in subflders including excel attached file. had below example excel vba not working kindly advie me
sub saveattachmentstofolder() ' outlook macro checks named subfolder in outlook inbox ' (here "daily final" folder) messages attached ' files of specific type (here file "xls" extension) ' , saves them disk. saved files timestamped. user ' can choose view saved files in windows explorer. ' note: make sure specified subfolder , save folder exist ' before running macro. on error goto saveattachmentstofolder_err ' declare variables dim ns namespace dim inbox mapifolder dim subfolder mapifolder dim item object dim atmt attachment dim filename string dim integer dim varresponse vbmsgboxresult set ns = getnamespace("mapi") set inbox = ns.getdefaultfolder(olfolderinbox) set subfolder = inbox.folders("daily final") ' enter correct subfolder name. = 0 ' check subfolder messages , exit of none found if subfolder.items.count = 0 msgbox "there no messages in sales reports folder.", vbinformation , _ "nothing found" exit sub end if ' check each message attachments each item in subfolder.items each atmt in item.attachments ' check filename of each attachment , save if has "xls" extension if right(atmt.filename, 3) = "xls" ' path must exist! change folder name necessary. filename = "c:\email attachments\" & _ format(item.creationtime, "yyyymmdd_hhnnss_") & atmt.filename atmt.saveasfile filename = + 1 end if next atmt next item ' show summary message if > 0 varresponse = msgbox("i found " & & " attached files." _ & vbcrlf & "i have saved them c:\email attachments folder." _ & vbcrlf & vbcrlf & "would view files now?" _ , vbquestion + vbyesno, "finished!") ' open windows explorer display saved files if user chooses if varresponse = vbyes shell "explorer.exe /e,c:\email attachments", vbnormalfocus end if else msgbox "i didn't find attached files in mail.", vbinformation , "finished!" end if ' clear memory saveattachmentstofolder_exit: set atmt = nothing set item = nothing set ns = nothing exit sub ' handle errors saveattachmentstofolder_err: msgbox "an unexpected error has occurred." _ & vbcrlf & "please note , report following information." _ & vbcrlf & "macro name: getattachments" _ & vbcrlf & "error number: " & err.number _ & vbcrlf & "error description: " & err.description _ , vbcritical, "error!" resume saveattachmentstofolder_exit end sub
it seems have cut-and-pasted website not familiar vba. when paste code vba code wondow, highlight lines there problems. apply knowledge fix these problems. example, line above should 1 statement:
msgbox "there no messages in sales reports folder.", vbinformation , _ "nothing found"
like so:
msgbox "message", buttons, "title"
you can put statement on 3 lines have it, must use line continuation character (_), have one, need two.
msgbox "there no messages in sales reports folder.", _ vbinformation , _ "nothing found"
here
filename = "c:\email attachments\" & _ format(item.creationtime, "yyyymmdd_hhnnss_") &
you have &. statement cannot end &
the f1 key can helpful in these situations.
Comments
Post a Comment