vba - Checking if newly received email has an attachment -
i'm kinda new vba , i'm working on vba code check if new email received has attachment or not. if not. send email sender email sent has no attachments.
code attached.
option explicit sub checkattachment(item outlook.mailitem) dim outattachment outlook.attachments dim outerattachment attachment dim outapp object dim outmail object if outattachment = 0 set outapp = createobject("outlook.application") outapp.session.logon set outmail = outapp.createitem(0) 'on error resume next outmail 'recipient sender .to = "test@gmail.com" 'auto-reply should "re : subject of message .subject = "re : " .cc = "" .bcc = ""`enter code here` .body = "no attachment found" .display end end if on error goto 0 end sub tried tweaking , worked... problem allowing file types. want jpeg, tiff , pdf accepted other send message attachment invalid file type
code goes this
option explicit public sub checkattachment(item outlook.mailitem) dim olinspector outlook.inspector dim oldocument outlook.documentitem dim olselection outlook.selection dim objatt outlook.attachment dim ft filetypes dim olreply mailitem dim fileextension string fileextension = "jpeg, jpg, tiff, pdf" '// check attachment if item.attachments.count > 1 goto checkfiletype1 end if checkfiletype1: if item.attachments(item.attachments, ".tiff") goto checkfiletype2 end if checkfiletype2: if item.attachments(item.attachments, ".jpeg") goto checkfiletype3 end if checkfiletype3: if item.attachments(item.attachments, ".pdf") goto sendmail else exit sub end if sendmail: set olreply = item.reply '// reply if no attachment found olreply.body = "no attachment found. re-send email , ensure needed file attached." & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf & "this system generated message. no need reply. thank you." olreply.send set olinspector = nothing set oldocument = nothing set olselection = nothing end sub
this should work.
option explicit public sub checkattachment(item outlook.mailitem) dim olinspector outlook.inspector dim oldocument word.document dim olselection word.selection dim olreply mailitem '// check attachment if item.attachments.count > 0 exit sub else set olreply = item.reply '// reply if no attachment found olreply.display end if set olinspector = application.activeinspector() set oldocument = olinspector.wordeditor set olselection = oldocument.application.selection olselection.insertbefore "no attachment found, thank you." '// send olreply.send set olinspector = nothing set oldocument = nothing set olselection = nothing end sub
Comments
Post a Comment