vba - Save Outlook attachment to disk -



vba - Save Outlook attachment to disk -

i found numerous examples of vba scripts automatically move attachments hard drive. 1 i've found online works when run macro in outlook is, not work when set rule.

when run macro without "item outlook.mailitem" parameter in sub header , have email containing file want saved selected, function properly.

however, add together info can run rule, outlook throws error , disables rule.

option explicit public sub moveattachmentsalpha(item outlook.mailitem) dim objol outlook.application dim objmsg outlook.mailitem 'object dim objattachments outlook.attachments dim objselection outlook.selection dim long dim lngcount long dim strfile string dim strfolderpath string dim strdeletedfiles string ' path documents folder strfolderpath = "c:\dailyflash\" on error resume next ' instantiate outlook application object. set objol = createobject("outlook.application") ' collection of selected objects. set objselection = objol.activeexplorer.selection ' check each selected item attachments. if attachments exist, ' save them strfolderpath folder , strip them item. each objmsg in objselection ' code strips attachments mail service items. ' if objmsg.class=olmail ' attachments collection of item. set objattachments = objmsg.attachments lngcount = objattachments.count strdeletedfiles = "" if lngcount > 0 ' need utilize count downwards loop removing items ' collection. otherwise, loop counter gets ' confused , every other item removed. = lngcount 1 step -1 ' save attachment before deleting item. ' file name. strfile = objattachments.item(i).filename ' combine path temp folder. strfile = strfolderpath & strfile ' save attachment file. objattachments.item(i).saveasfile strfile 'write save path string add together message 'check html , utilize html tags in link if objmsg.bodyformat <> olformathtml strdeletedfiles = strdeletedfiles & vbcrlf & "<file://" & strfile & ">" else strdeletedfiles = strdeletedfiles & "<br>" & "<a href='file://" & _ strfile & "'>" & strfile & "</a>" end if 'use msgbox command troubleshoot. remove final code. 'msgbox strdeletedfiles next ' adds filename string message body , save ' check html body if objmsg.bodyformat <> olformathtml objmsg.body = vbcrlf & "the file(s) saved " & strdeletedfiles & vbcrlf & objmsg.body else objmsg.htmlbody = "<p>" & "the file(s) saved " & strdeletedfiles & "</p>" & objmsg.htmlbody end if objmsg.save end if next exitsub: set objattachments = nil set objmsg = nil set objselection = nil set objol = nil end sub

keep of script. remove reference outlook.selection , loop associated it. then, in it's place, assign item objmsg allow rest of of script function normal. after testing have decided steal , utilize myself well.

public sub moveattachmentsalpha(item outlook.mailitem) dim objmsg outlook.mailitem 'object dim objattachments outlook.attachments dim long dim lngcount long dim strfile string dim strfolderpath string dim strdeletedfiles string ' path documents folder strfolderpath = "c:\temp\" on error resume next set objmsg = item ' code strips attachments mail service items. ' if objmsg.class=olmail ' attachments collection of item. set objattachments = objmsg.attachments lngcount = objattachments.count strdeletedfiles = "" if lngcount > 0 ' need utilize count downwards loop removing items ' collection. otherwise, loop counter gets ' confused , every other item removed. = lngcount 1 step -1 ' save attachment before deleting item. ' file name. strfile = objattachments.item(i).filename ' combine path temp folder. strfile = strfolderpath & strfile ' save attachment file. objattachments.item(i).saveasfile strfile 'write save path string add together message 'check html , utilize html tags in link if objmsg.bodyformat <> olformathtml strdeletedfiles = strdeletedfiles & vbcrlf & "<file://" & strfile & ">" else strdeletedfiles = strdeletedfiles & "<br>" & "<a href='file://" & _ strfile & "'>" & strfile & "</a>" end if next ' adds filename string message body , save ' check html body if objmsg.bodyformat <> olformathtml objmsg.body = vbcrlf & "the file(s) saved " & strdeletedfiles & vbcrlf & objmsg.body else objmsg.htmlbody = "<p>" & "the file(s) saved " & strdeletedfiles & "</p>" & objmsg.htmlbody end if objmsg.save end if exitsub: set objattachments = nil set objmsg = nil set objselection = nil set objol = nil end sub

fyi: changed nil after line ' code strips attachments mail service items. except next

vba outlook

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -