Skip to main content

如何在Outlook中將兩個資料夾合併到一個資料夾且不包含重複項目?

Author: Sun Last Modified: 2025-05-13

在Outlook中,您可能有多個郵件帳號和許多資料夾。在某些情況下,您希望將兩個資料夾合併為一個,並刪除重複的項目。該如何快速完成這項工作呢?本文介紹了一段VBA代碼,幫助您在Outlook中盡快處理此問題。

使用VBA代碼合併兩個資料夾且不包含重複項目

使用Kutools for Outlook合併多個資料夾並刪除重複項目


使用VBA代碼合併兩個資料夾且不包含重複項目

要將兩個資料夾合併為一個並刪除重複項目,您可以按照以下步驟使用VBA代碼來處理:

1. 按下「Alt + F11」鍵打開「Microsoft Visual Basic for Applications」窗口。

2. 在左側窗格中雙擊「ThisOutlookSession」,然後將以下代碼複製並粘貼到右側的腳本窗口中。

VBA:合併兩個資料夾且不包含重複項目

Sub MergeOutlookFolders_WithoutDuplicates()
'UpdatebyExtendoffice20180521
Dim xSourceFolder As Outlook.Folder
Dim xTargetFolder As Outlook.Folder
Dim xCount, i As Long
Dim xItem As Object
Dim xSourceItem As Object
Dim xTargetItem As Object
Dim xDictionary As Scripting.Dictionary  'Object
Dim xStr As String
On Error Resume Next
Set xDictionary = New Scripting.Dictionary
Set xSourceFolder = Application.Session.PickFolder
Set xTargetFolder = Application.Session.PickFolder
xCount = 0
If xSourceFolder.DefaultItemType <> xTargetFolder.DefaultItemType Then
    MsgBox "Error: The two folders are not in same type!", vbExclamation + vbOKOnly, "Kutools for Outlook"
    Exit Sub
End If
For i = xSourceFolder.Items.Count To 1 Step -1
    Set xSourceItem = xSourceFolder.Items.Item(i)
    xSourceItem.Move xTargetFolder
Next
For i = xTargetFolder.Items.Count To 1 Step -1
    Set xTargetItem = xTargetFolder.Items.Item(i)
    Select Case xTargetItem.Class
        Case olMail
            With xTargetItem
                xStr = .Subject & .Body & .SentOn
            End With
        Case olAppointment
            With xTargetItem
                xStr = .Subject & .Start & .Duration & .Location & .Body
            End With
        Case olContact
            With xTargetItem
                xStr = .FullName & .Email1Address & .Email2Address & .Email3Address
            End With
        Case olTask
            With xTargetItem
                xStr = .Subject & .StartDate & .DueDate & .Body
            End With
    End Select
    If xDictionary.Exists(xStr) = True Then
        xTargetItem.Delete
        xCount = xCount + 1
    Else
        xDictionary.Add xStr, True
    End If
Next i
If xCount <> 0 Then
    MsgBox xCount & " duplicates removed when merging!", vbInformation + vbOKOnly, "Kutools for Outlook"
End If
End Sub

doc merge folders without duplicates 1

3. 點擊「工具」>「引用」,在彈出的對話框中勾選「Microsoft Scripting Runtime」核取方塊。

doc merge folders without duplicates 2 doc arrow right doc merge folders without duplicates 3

4. 點擊「確定」。現在按下「F5」鍵運行代碼。會彈出一個對話框提醒您選擇要合併的第一個資料夾(注意:第一個資料夾中的所有項目在與第二個資料夾合併後將被移除)。
doc merge folders without duplicates 4

5. 點擊「確定」。在第二個彈出的對話框中,選擇要比較並合併的第二個資料夾。
doc merge folders without duplicates 5

6. 點擊「確定」。現在,第一個資料夾中的所有項目都將被移動到第二個資料夾,並且重複的項目將被刪除。
doc merge folders without duplicates 6


使用Kutools for Outlook合併多個資料夾並刪除重複項目

如果您不熟悉VBA代碼,可以嘗試使用「Kutools for Outlook」,這是一個方便的外掛程式,其「整合資料夾」和「重複郵件」功能可以輕鬆快速地處理此任務。

使用 Kutools for Outlook 解鎖極致郵件效率!永久免費獲取 70 項強大功能。立即下載免費版本

合併多個資料夾

1. 點擊「Kutools Plus」>「整合資料夾」,然後在「整合多個資料夾到一個」對話框中,點擊「新增」以將要合併的資料夾添加到列表中,並選擇一個資料夾作為目標資料夾。
doc consolidate and remove duplicate 1

2. 點擊「確定」>「確定」。現在,所選資料夾中的所有項目都已合併到指定的資料夾中。
doc consolidate and remove duplicate 2

刪除重複項目

3. 點擊「Kutools」>「刪除重複」>「重複郵件」。然後在「重複郵件」對話框中,勾選要刪除重複項的資料夾。
doc consolidate and remove duplicate 3

4. 點擊「下一步」。在「重複郵件設定」對話框中,指定用於比較郵件的條件。然後勾選「刪除重複郵件」選項,並選擇「單個文件夾內對比」。
doc consolidate and remove duplicate 4

5. 點擊「下一步」>「下一步」,重複的郵件將被過濾出來。點擊「刪除重複郵件」按鈕 >「確定」以成功刪除重複的郵件。
doc consolidate and remove duplicate 5 doc merge folders without duplicates 6