2017-11-29 6 views
0

아무도 내가 아래에있는 곳으로 잘못 갔는지 설명 할 수 있습니까?myItems.sort는 하나의 Outlook 하위 폴더에서 작동하지만 다른 사람에게는 dosnt가 적용됩니다.

코드는 하나의 하위 폴더 (최신 첨부 파일 추출)에서 작동하지만 다른 하위 폴더에 적용될 때 최신 하위 폴더가 아니라 가장 오래된 전자 메일에서 정보를 추출합니다.

'myFolder.Items.sort'가 올바른 방법입니까?

어떤 피드백이라도 대단히 감사하겠습니다.

Sub SaveAttachments_RsConfirmation() 
Dim myOlapp As Outlook.Application 
Dim myNameSpace As Outlook.Namespace 
Dim myFolder As Outlook.MAPIFolder 
Dim myItem As Outlook.MailItem 
Dim myAttachment As Outlook.Attachment 
Dim I As Long 

Set myOlapp = CreateObject("Outlook.Application") 
Set myNameSpace = myOlapp.GetNamespace("MAPI") 
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox) 
Set myFolder = myFolder.Folders("Rs.Confirmation") 


myFolder.Items.Sort "[ReceivedTime]", True 

For Each myItem In myFolder.Items 
myFolder.Items.Sort "[ReceivedTime]", True 
    If myItem.Attachments.Count <> 0 Then 
     For Each myAttachment In myItem.Attachments 

      I = 1 
      myAttachment.SaveAsFile "C:\Del.Gen.v1\Confirmation.Email\" & I & ".txt" 
      eSender = myItem.SenderEmailAddress 
      dtRecvd = myItem.ReceivedTime 
      dtSent = myItem.CreationTime 
      sSubj = myItem.Subject 
      sMsg = myItem.Body 

      Exit For 
     Next 
    End If 
Next 



Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A1").Value = eSender 
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A2").Value = dtRecvd 
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A3").Value = dtSent 
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A4").Value = sSubj 
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A5").Value = sMsg 



Debug.Print eSender 
Debug.Print dtRecvd 
Debug.Print dtSent 
Debug.Print sSubj 
Debug.Print sMsg 



End Sub 

답변

0

당신은 하나 명의 Items 컬렉션을 정렬하지만, 완전히 다른 개체를 사용하게된다 - 당신이 MAPIFolder.Items를 호출 할 때마다, 당신은 다른 인스턴스에 대한 지식이없는 새로운 COM 개체를 다시 얻을. 항목 컬렉션을 한 번 읽고 변수에 저장 한 다음 항목을 반복합니다.

set myItems = myFolder.Items 
myItems.Sort "[ReceivedTime]", True 
For Each myItem In myItems 
    ... 
+0

피드 백 Dmitry 주셔서 감사. 정말로 도움이된다!! – Michael

0

폴더를 정렬하고 각 항목을 가져 와서 전체 폴더를 다시 정렬합니다. 이것은 아마도 당신이 의미하는 바가 아닙니다. I이 증가되지 않은 경우 폴더의 마지막 이메일에서 하나가 될 것입니다 하나 개의 첨부 파일을 볼 수 있도록, 모든 attachements가 동일한 파일에 저장 (대체)됩니다

myFolder.Items.Sort "[ReceivedTime]", True   ' sort folder 

For Each myItem In myFolder.Items     ' process each item 
    myFolder.Items.Sort "[ReceivedTime]", True  ' sort folder again: remove this line 
    If myItem.Attachments.Count <> 0 Then 
     For Each myAttachment In myItem.Attachments 
      I = 1          ' shouldn't this be incremented? 

참고.

귀하의 문제/질문이 확실하지 않습니다. 먼저 두 번째 정렬 명령을 제거합니다. 재 분류는 컬렉션에서 myItem을 얻은 순서를 엉망으로 만들 수 있습니다.

그러나 각 하위 폴더와 하위 폴더 등으로 들어가려면 폴더를 정렬하고 각 항목을 처리하며 항목이 폴더이면 하위 폴더로 내림하고 folderr을 정렬하는 재귀 프로 시저를 개발해야합니다.

+0

"내가 증가하지 않으면 모든 첨부 파일이 동일한 파일에 배치 (교체)되므로 볼 수 있습니다. 하나의 첨부 파일은 폴더의 마지막 이메일에있는 것입니다. " ---- 이것은 매우 잘 내 문제가 될 수 있습니다 폴, 많은 변형을 시도하고 서클에서 라운드 갔다, 당신은 내 자신의 배꼽 vba에서 경험 한 것처럼 :) – Michael

0

myItems.Sort가 좋을 것입니다.

myFolder.Items.Sort는 본 문서에도 불구하고 유효하지 않습니다.

Sub SaveAttachments_RsConfirmation() 

Dim myOlapp As Outlook.Application 
Dim myNameSpace As Outlook.NameSpace 
Dim myFolder As Outlook.MAPIFolder 

Dim myItems As Outlook.items 

Set myOlapp = CreateObject("Outlook.Application") 
Set myNameSpace = myOlapp.GetNamespace("MAPI") 
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox) 

Set myFolder = myFolder.folders("Rs.Confirmation") 

' Attempt to sort items in the folder 
myFolder.items.Sort "[ReceivedTime]", True 
Debug.Print myFolder.items(1).Subject 

' False should be no different from True 
myFolder.items.Sort "[ReceivedTime]", False 
Debug.Print myFolder.items(1).Subject 

' Create a collection of items 
Set myItems = myFolder.items 
myItems.Sort "[ReceivedTime]", True 
Debug.Print myItems(1).Subject 

' False should sort opposite to True 
myItems.Sort "[ReceivedTime]", False 
Debug.Print myItems(1).Subject 

ExitRoutine: 
    Set myOlapp = Nothing 
    Set myNameSpace = Nothing 
    Set myFolder = Nothing 
    Set myItems = Nothing 

End Sub