vba - Macro fails for large sets of items -
i have functioning macro fails @ seemingly random points when running on large number of items. macro used loop through inbox folder receives error logs, save error log text files, copy specified lines of text attachments (error operation names , such), place these strings in excel file track them, , move email items inbox folder once processed. works great when goes through less hundred emails above gets strange. in testing failed on 122nd iteration, 648, 350, etc. general structure below.
sub errorlogauto() dim filename string dim path string dim timeinfo string dim subjectinfo string dim idnumber string dim dataline string dim oitem object dim item outlook.items dim myattachment(1000) outlook.attachments dim myinspector outlook.inspector dim appexcel object dim filenum integer dim found integer dim found1 integer dim found2 integer dim integer dim j integer dim op integer dim integer dim cdata integer = 0 k = 1 'returns proper source folder set mynamespace = application.getnamespace("mapi") set myfolder = mynamespace.getdefaultfolder(olfolderinbox) set mynewfolder = myfolder.folders("test") '--> text between "" folder name, change here 'set path attachments saved in path = "c:\test\" 'set item = emails in test folder set item = mynewfolder.items 'if no emails... if item.count = 0 msgbox "there no error messages sift through." exit sub end if 'open instance of excel workbook set appexcel = createobject("excel.application") appexcel.visible = true 'appexcel.workbooks.open (path & "test.xlsx") appexcel.workbooks.open (path & "sample file name.xlsx") 'find first empty cell write --> based off of column d while appexcel.range("d" & k) <> "" k = k + 1 wend 'for every email in folder...here starts big loop each oitem in item 'save attachment , set filename set myattachment(i) = oitem.attachments myattachment(i).item(1).saveasfile path & myattachment(i).item(1).displayname & ".txt" filename = path & myattachment(i).item(1).displayname & ".txt" 'subject , time info subjectinfo = oitem.subject timeinfo = oitem.receivedtime 'returns id number subject string after '@' j = instr(subjectinfo, "@") idnumber = mid(subjectinfo, j + 1) 'write idnumber cell , timestamp appexcel.range("a" & k) = timeinfo appexcel.range("d" & k) = idnumber 'open notepad file, read line line until eof, take user message, , take operation name filenum = freefile() open filename input #filenum while not eof(filenum) line input #filenum, dataline 'if string found these <> 0 found = instr(dataline, "<operationname>") found1 = instr(dataline, "<usermessage>") found2 = instr(dataline, "<usermessage><![cdata[") 'returns position right after string found op = instr(dataline, "<operationname>") + 15 = instr(dataline, "<usermessage>") + 13 cdata = instr(dataline, "<usermessage><![cdata[") + 22 'found operation name line if found <> 0 'appexcel.range("b1") = dataline --> whole line 'appexcel.range("c" & k) = mid(mid(dataline, 20), 1, len(mid(dataline, 20)) - 16) --> doesnt account whitespace appexcel.range("n" & k) = mid(mid(dataline, op), 1, len(mid(dataline, op)) - 16) '--> accounts whitespace , cuts out <operationname> , <\operationname> 'found user message line , includes cdata stuff elseif found1 <> 0 , found2 <> 0 'appexcel.range("c1") = dataline --> whole line 'appexcel.range("d" & k) = mid(mid(dataline, 20), 1, len(mid(dataline, 20)) - 14) --> doesnt account whitespace 'appexcel.range("o" & k) = mid(mid(dataline, us), 1, len(mid(dataline, us)) - 14) --> accounts whitespace , cuts out <usermessage> , <\usermessage> appexcel.range("o" & k) = mid(mid(dataline, cdata), 1, len(mid(dataline, cdata)) - 17) '--> accounts whitespace , cuts out <usermessage><![cdata[ , ]]><\usermessage> 'found user message line without cdata stuff elseif found1 <> 0 appexcel.range("o" & k) = mid(mid(dataline, us), 1, len(mid(dataline, us)) - 14) '--> accounts whitespace , cuts out <usermessage> , <\usermessage> end if wend close #filenum = + 1 k = k + 1 next call foldermove end sub private sub foldermove() dim mailitem dim m integer dim source mapifolder dim destination mapifolder set source = application.getnamespace("mapi").getdefaultfolder(olfolderinbox) set source = source.folders("test") '--> text between "" folder name, change here set destination = application.getnamespace("mapi").getdefaultfolder(olfolderinbox) set destination = destination.folders("testing done") '--> text between "" folder name, change here m = source.items.count 1 step -1 set = source.items(m) a.move destination next end sub
the code breaks down while reading file in not eof loop. these errors caused bad programming practices? i've never worked large sets before , new vba appreciated.
error information: run-time error '50290': application-defined or object defined error. --> occured on 363rd iteration
restarted @ debug , got 540 before failing in same way.
then restarted , finished ok.
so question why happen?
in online profile (as opposed cached), exchange limit number off items can open (250 default. need make sure explicitly release objects setting them northing (vba) or calling marshal.releasecomobject in .net. should make sure not use multipole dot notation avoid implicit variable cannot explicitly release.
for = 1 item.count set oitem = item.items(i) set oattachments = oitem.attachments if oattachments.count > 0 set oattachment = oattachments.item(1) ' want loop through attachments? filename = path & oattachment.filename oattachment.saveasfile filename set oattachment = nothing end if ... set oattachments = nothing set oitem = nothing next
Comments
Post a Comment