【问题标题】:Copying Outlook folder structure to Windows explorer将 Outlook 文件夹结构复制到 Windows 资源管理器
【发布时间】:2020-05-27 16:39:05
【问题描述】:

这是我执行此任务的代码。问题在于 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 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
    xItem = ReplaceInvalidCharacters(xItem.Item)
    xSubject = ReplaceInvalidCharacters(xItem.Subject)
    xSubFld = ReplaceInvalidCharacters(xItem.SubFld)
    xFldPath = ReplaceInvalidCharacters(xItem.FldPath)
    xPath = ReplaceInvalidCharacters(xItem.Path)
    xSubject = ReplaceInvalidCharacters(xItem.Subject)
    xFilename = xSubject & ".msg"
    xFilename = ReplaceInvalidCharacters(xItem.FileName)
    xFilePath = xPath & "\" & xFilename
    xFilePath = ReplaceInvalidCharacters(xItem.FilePath)
    If xFSO.FileExists(xFilePath) Then
        xCounter = xCounter + 1
        xFilename = xSubject & " (" & xCounter & ").msg"
        xFilePath = xPath & "\" & xFilename
        xFilePath = ReplaceInvalidCharacters(xItem.FilePath)
    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

【问题讨论】:

    标签: vba directory outlook subdirectory


    【解决方案1】:
    1. 始终在每个模块的最顶部使用Option Explicit,在任何功能之前。这将告诉您是否没有Dim'med 任何变量。在这种情况下,xCount 和 xCounter 存在问题,它们应该只有一个名称。

    2. 我认为问题可能来自函数ExportOutlookFolder,这一行:
      xPath = xFldPath &amp; "\" &amp; OutlookFolder.Name
      尝试将其替换为:
      xPath = xFldPath &amp; "\" &amp; ReplaceInvalidCharacters(OutlookFolder.Name)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多