vba - Error 440 "Array Index out of Bounds" -
i trying download excel attachment subject keyword.
i managed create code giving error 440 "array index out of bounds"
.
the code got stuck in part.
if items(i).class = outlook.olobjectclass.olmail
here code
sub attachment() dim n1 string dim en string en = cstr(environ("userprofile")) savefolder = en & "\desktop\" n1 = "mail attachment" if len(dir(savefolder & n1, vbdirectory)) = 0 mkdir (savefolder & n1) end if call test01 end sub private sub test01() dim inbox outlook.folder dim obj object dim items outlook.items dim attach object dim mailitem outlook.mailitem dim long dim filter string dim savefolder string, pathlocation string dim dateformat string dim datecreated string dim strnewfoldername string dim creation string const filetype1 string = "xlsx" const filetype2 string = "xlsm" const filetype3 string = "xlsb" const filetype4 string = "xls" dim env string env = cstr(environ("userprofile")) savefolder = env & "\desktop\mentor training\" set inbox = application.getnamespace("mapi").getdefaultfolder(olfolderinbox) 'if inbox.items.restrict("[unread] = true").count = 0 ' msgbox "no mentor training mail in inbox" ' exit sub 'end if filter = "@sql=" & chr(34) & "urn:schemas:httpmail:datereceived" & _ chr(34) & " >= '4/2/2017' , " & _ chr(34) & "urn:schemas:httpmail:hasattachment" & _ chr(34) & "=1 and" & chr(34) & _ chr(34) & "urn:schemas:httpmail:read" & _ chr(34) & "= 0" set items = inbox.items.restrict(filter) = 1 items.count if items(i).class = outlook.olobjectclass.olmail set obj = items(i) debug.print obj.subject each attach in obj.attachments if right(lcase(attach.filename), len(filetype1)) = filetype1 'for searching excel files dateformat = format(obj.receivedtime(), "dd-mm-yyyy hh-mm") attach.saveasfile savefolder & "(" & dateformat & ")" & " " & attach end if if right(lcase(attach.filename), len(filetype2)) = filetype2 'for searching excel files dateformat = format(obj.receivedtime(), "dd-mm-yyyy hh-mm") attach.saveasfile savefolder & "(" & dateformat & ")" & " " & attach end if if right(lcase(attach.filename), len(filetype3)) = filetype3 'for searching excel files dateformat = format(obj.receivedtime(), "dd-mm-yyyy hh-mm") attach.saveasfile savefolder & "(" & dateformat & ")" & " " & attach end if if right(lcase(attach.filename), len(filetype4)) = filetype4 'for searching excel files dateformat = format(obj.receivedtime(), "dd-mm-yyyy hh-mm") attach.saveasfile savefolder & "(" & dateformat & ")" & " " & attach end if obj.unread = false doevents obj.save next end if next msgbox "attachment saved" end sub
it understanding arrays in vba started @ 0 default. if there 1 item in list located @ items(0). , since statement starts looking @ items(1) throw error. changing to:
for = 0 items.count - 1
should work believe.
Comments
Post a Comment