vba - Loop through to copy multiple outlook attachments type mismatch error -
i trying loop through message copy multiple attachments, no success, i'm getting 13 - type mismatch error!!!
any suggestions appreciated.
my code follows,
private sub items_itemadd(byval item object) on error goto errorhandler 'only act if it's mailitem dim msg outlook.mailitem if typename(item) = "mailitem" set msg = item 'set folder save in. dim oldestfldr outlook.mapifolder dim myattachments outlook.attachments dim att string dim integer 'location save in. can root drive or mapped network drive. const attpath string = "c:\users\pkshahbazi\documents\emailattachments\" = 0 'save attachment set myattachments = item.attachments if msg.attachments.count <> 0 each myattachments in msg.attachments att = myattachments.item(i).displayname myattachments.item(i).saveasfile attpath & att 'mark read = + 1 next myattachments msg.unread = false end if end if programexit: exit sub errorhandler: msgbox err.number & " - " & err.description resume programexit end sub
you're using indexed loop on iterable collection (myattachments) unnecessary. not source of error, error see related iteration:
you're getting mismatch error because you're doing:
for each myattachments in msg.attachments '... next
you have assigned myattachments
msg.attachments
, of type outlook.attachments
. iterable collection, need iterate using outlook.attachment
type variable.
dim olattch outlook.attachment
then, modify loop (untested):
set myattachments = msg.attachments each olattch in myattachments att = olattch.displayname olattch.saveasfile attpath & att next olattch msg.unread = false
Comments
Post a Comment