VBA를 통해 Outlook에서 재귀 적으로 폴더 트리를 탐색하는 방법

지금 공유 :

기본 받은 편지함 또는 보낸 항목 폴더 내에 하위 폴더가 있는 경우 VBA를 사용하여 재귀적이고 효율적인 방법으로 하위 폴더를 탐색하는 방법은 매우 유용한 팁입니다. 이 중요한 부분은 다른 많은 VBA 프로젝트에서 재사용할 수 있습니다.

폴더 이동의 중요한 문제

Outlook의 폴더 트리Corporate and commercial users of Outlook often end up having hundreds of subfolders beneath their main inbox or sent items folder, with relevant emails in each of those. When there comes a need to go through all those folders to perform some action on the emails contained in those, it becomes a nightmare to write different VBA scripts for different folders. Imagine having five hundred folders beneath the main Inbox folder and a VBA script requirement for each of those. The user will have to write five hundred unique scripts for each of those folders, with the similar functionality. To make it worse, if there comes a need to make some modifications, all five hundred scripts will be required to be updated. Luckily there is a programming methodology known as recursion, which solves our problem like a breeze. The idea of recursion is for a function to repeatedly call itself and go through the same pattern again and again, in order to process all the items, with the same logic. Below is a sample script which starts from the main Inbox folder and iterates through all the subfolders and items in each of those folders.

Outlook VBA 스크립트

다음은 전체 Outlook VBA 스크립트입니다.

Private Sub Main()
    Dim objNameSpace As Outlook.NameSpace
    Dim objMainFolder As Outlook.Folder
    
    Set objNameSpace = Application.GetNamespace("MAPI")
    Set objMainFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
    
    Call ProcessCurrentFolder(objMainFolder)
End Sub
 
Private Sub ProcessCurrentFolder(ByVal objParentFolder As Outlook.MAPIFolder)
    Dim objCurFolder As Outlook.MAPIFolder
    Dim objMail As Outlook.MailItem
    
    On Error Resume Next
    
    '    Process each items in the folder
    For Each objMail In objParentFolder.Items
        '    Do your task here ...
    Next
    
    '    Process the subfolders in the folder recursively
    If (objParentFolder.Folders.Count > 0) Then
        For Each objCurFolder In objParentFolder.Folders
            Call ProcessCurrentFolder(objCurFolder)
        Next
    End If
End Sub

스크립트 실행 방법

PR 기사 Alt + F11 to open the Outlook VBA editor and paste the above code in “ThisOutlookSession”. It is important to know that prior to running this script, Microsoft Outlook XX Object library should be added to the project references from “Tools” menus, where XX is the library version.

스크립트 이해

재귀 함수“ProcessCurrentFolder” is the recursive function, which calls itself, if a subfolder is discovered within the current folder. It will keep on recalling itself unless all the folders starting from initial hierarchy are scanned and processed. This script will work with any Outlook folder. “olFolderInbox” can be replaced with “olFolderSentMail”, “olFolderOutbox”, or can be any other non-default Outlook folder. It is also important to know that it is not necessary to make the initial call from the root inbox or outbox folder but it can start working from any folder downwards. “For Each objMail In objParentFolder.Items” is the key line which will iterate through all the items contained in the current folder being scanned; therefore that is the place where the user needs to put the required code to process the items.

재해 복구

If you are faced with a situation of Outlook crash or failure, there is no need to worry about the lost emails, as DataNumen 당신의 삶을 정상으로 되돌려 줄 것입니다. Outlook 손상 수정 효율적인 방식으로.

저자 소개 :

Mary Underwood는 다음 분야의 데이터 복구 전문가입니다. DataNumen, Inc.는 다음과 같은 데이터 복구 기술 분야의 세계적 리더입니다. dwg recovery rar 복구 소프트웨어 제품. 자세한 내용은 WWW.datanumen.COM

지금 공유 :

댓글이 닫혀있다.