Skip to main content

如何將 Outlook 資料夾結構複製到桌面(Windows 資源管理器)?

Author: Kelly Last Modified: 2025-05-12

眾所周知,我們可以使用封存功能將資料夾結構複製到另一個 Outlook,但您是否知道如何將 Outlook 的資料夾結構複製到特定的 Windows 資料夾,例如桌面呢?本文將介紹一種 VBA 方法,讓您輕鬆將 Outlook 資料夾結構複製到 Windows 資源管理器。

將 Outlook 資料夾結構複製到桌面(Windows 資源管理器)


將 Outlook 資料夾結構複製到桌面(Windows 資源管理器)

請按照以下步驟將 Outlook 資料夾結構複製到桌面或 Windows 資源管理器。

1. 在導覽窗格中,請點擊以突出顯示要複製其資料夾結構的指定資料夾,然後按下「Alt」+「F11」鍵以打開 Microsoft Visual Basic for Applications 窗口。

the screenshot of step about copying Outlook folder structure to desktop (windows explorer) using vba 1

2. 點擊「工具」>「引用」以打開引用對話框。然後在對話框中勾選「Microsoft Scripting Runtime」選項,並點擊「確定」按鈕。參見截圖:

the screenshot of step about copying Outlook folder structure to desktop (windows explorer) using vba 2

3. 點擊「插入」>「模組」,並將以下 VBA 代碼複製並粘貼到新的模組窗口中。

VBA:將 Outlook 資料夾結構複製到 Windows 資源管理器

Dim xFSO As Scripting.FileSystemObject
Sub CopyOutlookFldStructureToWinExplorer()
    ExportAction "Copy"
End Sub
  
Sub ExportAction(xAction As String)
Dim xFolder As Outlook.Folder
Dim xFldPath As String
xFldPath = SelectAFolder()
If xFldPath = "" Then
    MsgBox "You did not select a folder. Export cancelled.", vbInformation + vbOKOnly, "Kutools for Outlook"
Else
    Set xFSO = New Scripting.FileSystemObject
    Set xFolder = Outlook.Application.ActiveExplorer.CurrentFolder
    ExportOutlookFolder xFolder, xFldPath
End If
Set xFolder = Nothing
Set xFSO = Nothing
End Sub

Sub ExportOutlookFolder(ByVal OutlookFolder As Outlook.Folder, xFldPath As String)
Dim xSubFld As Outlook.Folder
Dim xItem As Object
Dim xPath As String
Dim xFilePath As String
Dim xSubject As String
Dim xCount As Integer
Dim xFilename As String
On Error Resume Next
xPath = xFldPath & "\" & OutlookFolder.Name
'?????????,??????
If Dir(xPath, 16) = Empty Then MkDir xPath
For Each xItem In OutlookFolder.Items
    xSubject = ReplaceInvalidCharacters(xItem.Subject)
    xFilename = xSubject & ".msg"
    xCount = 0
    xFilePath = xPath & "\" & xFilename
    If xFSO.FileExists(xFilePath) Then
        xCount = xCount + 1
        xFilename = xSubject & " (" & xCount & ").msg"
        xFilePath = xPath & "\" & xFilename
    End If
    xItem.SaveAs xFilePath, olMSG
Next
For Each xSubFld In OutlookFolder.Folders
    ExportOutlookFolder xSubFld, xPath
Next
Set OutlookFolder = Nothing
Set xItem = Nothing
End Sub

Function SelectAFolder() As String
Dim xSelFolder As Object
Dim xShell As Object
On Error Resume Next
Set xShell = CreateObject("Shell.Application")
Set xSelFolder = xShell.BrowseForFolder(0, "Select a folder", 0, 0)
If Not TypeName(xSelFolder) = "Nothing" Then
    SelectAFolder = xSelFolder.self.Path
End If
Set xSelFolder = Nothing
Set xShell = Nothing
End Function
  
Function ReplaceInvalidCharacters(Str As String) As String
Dim xRegEx
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "\||\/|\<|\>|""|:|\*|\\|\?"
ReplaceInvalidCharacters = xRegEx.Replace(Str, "")
End Function

4. 按下「F5」鍵或點擊「運行」按鈕來執行此 VBA。

5. 在彈出的「瀏覽資料夾」對話框中,請選擇要放置複製的資料夾結構的指定資料夾,然後點擊「確定」按鈕。參見截圖:

the screenshot of step about copying Outlook folder structure to desktop (windows explorer) using vba 3

現在進入指定的資料夾,您會看到資料夾結構已複製到指定的硬碟中。參見截圖:

the screenshot of step about copying Outlook folder structure to desktop (windows explorer) using vba 4

注意:資料夾中的項目,例如郵件、約會、任務等,也會被複製到硬碟中相應的資料夾中。


相關文章

如何在 Outlook 中將資料夾結構複製到新的 pst 數據文件?