Advertisement
Guest User

Export Outlook Folders to the File System

a guest
Sep 4th, 2014
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'On the next line edit the starting folder as desired.  If you leave it blank, then the starting folder will be the local computer.
  2. Const STARTING_FOLDER = ""
  3.  
  4. Dim objFSO As Object
  5.  
  6. Sub CopyOutlookFolderToFileSystem()
  7.     ExportController "Copy"
  8. End Sub
  9.  
  10. Sub MoveOutlookFolderToFileSystem()
  11.     ExportController "Move"
  12. End Sub
  13.  
  14. Sub ExportController(strAction As String)
  15.     Dim olkFld As Outlook.MAPIFolder, strPath As String
  16.     strPath = SelectFolder(STARTING_FOLDER)
  17.     If strPath = "" Then
  18.         MsgBox "You did not select a folder.  Export cancelled.", vbInformation + vbOKOnly, "Export Outlook Folder"
  19.     Else
  20.         Set objFSO = CreateObject("Scripting.FileSystemObject")
  21.         Set olkFld = Application.ActiveExplorer.CurrentFolder
  22.         ExportOutlookFolder olkFld, strPath
  23.         If LCase(strAction) = "move" Then olkFld.Delete
  24.     End If
  25.     Set olkFld = Nothing
  26.     Set objFSO = Nothing
  27. End Sub
  28.  
  29. Sub ExportOutlookFolder(ByVal olkFld As Outlook.MAPIFolder, strStartingPath As String)
  30.     Dim olkSub As Outlook.MAPIFolder, olkItm As Object, strPath As String, strMyPath As String, strSubejct As String, intCount As Integer
  31.     strPath = strStartingPath & "\" & RemoveIllegalCharacters(olkFld.Name)
  32.     objFSO.CreateFolder strPath
  33.     For Each olkItm In olkFld.Items
  34.         If UCase(olkItm.MessageClass) Like "*NOTE*" Then
  35.             If Not IsEmpty(olkItm.SenderName) Then
  36.                 If Not IsEmpty(olkItm.Subject) Then
  37.                     strSubject = RemoveIllegalCharacters(olkItm.SenderName) & " - " & RemoveIllegalCharacters(olkItm.Subject)
  38.                     strFilename = Trim(strSubject) & ".msg"
  39.                     intCount = 0
  40.                     Do While True
  41.                         strMyPath = strPath & "\" & strFilename
  42.                         If objFSO.FileExists(strMyPath) Then
  43.                             intCount = intCount + 1
  44.                             strFilename = strSubject & " (" & intCount & ").msg"
  45.                         Else
  46.                             Exit Do
  47.                         End If
  48.                     Loop
  49.                     If Len(strMyPath) >= 255 Then
  50.                         strMyPath = Left(strMyPath, 200)
  51.                     End If
  52.                     olkItm.SaveAs strMyPath
  53.                     ChangeTimeStamp strMyPath, olkItm.ReceivedTime
  54.                 End If
  55.             End If
  56.         End If
  57.     Next
  58.     For Each olkSub In olkFld.Folders
  59.         ExportOutlookFolder olkSub, strPath
  60.     Next
  61.     Set olkFld = Nothing
  62.     Set olkItm = Nothing
  63. End Sub
  64.  
  65. Function SelectFolder(varStartingFolder As Variant) As String
  66.     ' This function is a modified version of the SelectFolder function written by Rob van der Woude (http://www.robvanderwoude.com/vbstech_ui_selectfolder.php)
  67.  
  68.     ' Standard housekeeping
  69.    Dim objFolder As Object, objShell As Object
  70.      
  71.     ' Custom error handling
  72.    On Error Resume Next
  73.  
  74.     ' Create a dialog object
  75.    Set objShell = CreateObject("Shell.Application")
  76.     Set objFolder = objShell.BrowseForFolder(0, "Select the folder you want to export to", 0, varStartingFolder)
  77.  
  78.     ' Return the path of the selected folder
  79.    If TypeName(objFolder) <> "Nothing" Then SelectFolder = objFolder.self.Path
  80.  
  81.     ' Standard housekeeping
  82.    Set objFolder = Nothing
  83.     Set objShell = Nothing
  84.     On Error GoTo 0
  85. End Function
  86.  
  87. Function RemoveIllegalCharacters(strValue As String) As String
  88.     ' Purpose: Remove characters that cannot be in a filename from a string.'
  89.    ' Written: 4/24/2009'
  90.    ' Author:  BlueDevilFan'
  91.    ' Outlook: All versions'
  92.    RemoveIllegalCharacters = strValue
  93.     ' RemoveIllegalCharacters = LCase(RemoveIllegalCharacters)
  94.    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
  95.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
  96.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
  97.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
  98.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
  99.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
  100.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
  101.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
  102.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
  103.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "@", "_at_")
  104.     RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(9),"")
  105.     ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "û", "u")
  106.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ü", "u")
  107.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "à", "a")
  108.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ç", "c")
  109.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "é", "e")
  110.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "è", "e")
  111.    ' RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ê", "e")
  112. End Function
  113.  
  114. Sub ChangeTimeStamp(strFile As String, datStamp As Date)
  115.     Dim objShell As Object, objFolder As Object, objFolderItem As Object, varPath As Variant, varName As Variant
  116.     varName = Mid(strFile, InStrRev(strFile, "\") + 1)
  117.     varPath = Mid(strFile, 1, InStrRev(strFile, "\"))
  118.     Set objShell = CreateObject("Shell.Application")
  119.     Set objFolder = objShell.NameSpace(varPath)
  120.     Set objFolderItem = objFolder.ParseName(varName)
  121.     objFolderItem.ModifyDate = CStr(datStamp)
  122.     Set objShell = Nothing
  123.     Set objFolder = Nothing
  124.     Set objFolderItem = Nothing
  125. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement