여러분이 저를 도울 수 있는지 궁금합니다. 우리는 최근에 Outlook 2003에서 2010으로 옮겼습니다. 우리는 Outlook VBA 스크립트를 사용하여 특정 들어오는 전자 메일 양식을 읽고, 읽었으며, 정보를 추출하여 파일로 컴파일합니다.VBA 용 루프가 예상대로 작동하지 않습니다.
스크립트는 작동하지만 폴더의 절반 만 읽습니다. 나는 폴더에 대한 카운트 테스트를했고, 정확한 숫자를 보여 주지만, 어떤 이유로, for 루프는 아이템의 절반 만 실행한다. 폴더에 8 개의 메시지가있는 경우 4 개만 읽으면 4 개가 있으면 2 개만 읽습니다.
코드의 어느 부분이 폭탄인지 알 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다. 내 코드의 for 루프 부분 만 게시했습니다. 전체 스크립트가 필요한 경우 알려 주시기 바랍니다.
enter code here Set myOlApp = CreateObject("Outlook.Application") 'Outlook App Obj.
Set myNameSpace = myOlApp.GetNamespace("MAPI") 'MAPI Namespace
Set myFolder = myNameSpace.Folders("[email protected]").Folders("TestAccMail") 'Outlook folder to access
For Each Item In myFolder.Items 'Loop through each mail item
If (regex.Test(Item.Subject)) Then 'Test for TestAccX Message
strDataSplit = Split(Item.Body, vbNewLine) 'Split the contents of the body to an array
strOutput = ""
For Each arrItem In strDataSplit 'Loop through the contents of the e-mail body
If (regExData.Test(arrItem)) Then 'Test if line contains a field we need
field = Split(arrItem, ":")(1) 'Store the value of the field
strOutput = strOutput & Trim(Replace(field, Chr(160), "")) & "|" 'Concat the previous field value with current; seperated by |
End If
Next arrItem 'Next field in array
If Not strOutput = "" Then 'Ensure the output var has data
WriteToATextFile strOutput, file 'Append the data record to the provided file
Item.Move myFolder.Folders("TestAcc Complete") 'Move mail item to completed folder
recCount = recCount + 1
Else 'If the string is blank, no data was extracted; Error!
Item.Move myFolder.Folders("Errors") 'Move mail item to Error folder
errCount = errCount + 1 'Incremeant error count
End If
messCount = messCount + 1 'Incremeant message count, error or not
End If
Next 'Next TestAccX Message
앞에 '오류 재개'가 있습니까? for 루프가 올바른 횟수만큼 실행됩니까? 그렇다면 오류를 일으키는 regex 또는 strOutput이 있는지 확인하기 위해 코드를 추가 했습니까? –
Alex - "On Error GoTo outlookerror :"를 사용하고 있습니다. 오류가 발생하면 그에 대한 알림을 받게됩니다. 또한 For 루프는 필요한 시간의 절반 만 실행합니다. 디버그 모드로 설정하고 모든 코드를 단계별로 실행했습니다. 오류는 발생하지 않았고 루프는 필요한 시간의 절반 만 실행했습니다. – user2516507
글쎄, 당신은 folder.items의 각 항목에 대해 쓴 다음, 다른 폴더로 항목을 이동하는 것은 분명히 나쁜 습관입니다. 예를 들어, 첫 번째 항목에서 이동합니다. 이제 두 번째 항목으로 이동하지만 첫 번째 항목을 이동 했으므로 두 번째 항목은 첫 번째 색인을 가져 오지만 두 번째 색인은 이미 이동하여 첫 번째 항목을 무시합니다. 콜렉션에서 작업 할 때마다 항상 콜렉션을 사용하십시오. 콜렉션에서 항목을 이동하더라도 남아있는 항목의 색인은 변경되지 않습니다. 내 언어가 불분명하면 죄송합니다. – Vikas