Advertisement
chisiyuan

saveMsg of outlook

Jul 19th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Attribute VB_Name = "saveMsg"
  2. Public Sub SaveAtt()
  3.  
  4. Dim msg As MailItem
  5. Dim exp As Explorer
  6. Dim att As Attachment
  7. Dim attpath As String
  8. Dim htmlname As String
  9. Dim htmlpath As String
  10. Dim folder As String
  11. Dim msgdate As Date
  12. Dim msgday As String
  13. Dim charArray As Variant
  14. Dim tmpChar As Variant
  15. Dim changeTo As String
  16.  
  17. Set exp = Application.ActiveExplorer
  18. charArray = Array("?", "/", "\", ":", "*", """", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+")
  19.  
  20. ' the folder to save files; must ends with slash
  21. Set objShell = CreateObject("Shell.Application")
  22. Set objFolder = objShell.BrowseForFolder(0, "select a folder...", 0, 0)
  23. If objFolder Is Nothing Then
  24.     MsgBox "No folder is selected. Aborted!"
  25.     Exit Sub
  26. Else
  27.     folder = objFolder.self.path + "\"
  28.     'MsgBox folder
  29.    'Exit Sub
  30. End If
  31. Set objFolder = Nothing
  32. Set objShell = Nothing
  33.    
  34.    
  35. For Each msg In exp.Selection
  36.     'msg reception date for index
  37.    msgdate = msg.ReceivedTime
  38.     msgday = Day(msgdate)
  39.  
  40.     htmlname = CStr(msg.Subject)
  41.     ' remove illegal characters in subject
  42.    For Each tmpChar In charArray
  43.         Select Case tmpChar
  44.         Case "&"
  45.             changeTo = " and "
  46.         Case ":"
  47.          changeTo = "-"
  48.         Case Else
  49.             changeTo = " "
  50.         End Select
  51.         htmlname = Replace(htmlname, tmpChar, changeTo)
  52.      Next
  53.      
  54.     htmlpath = folder + CStr(msgday) + "_" + htmlname + ".html"
  55.     msg.SaveAs htmlpath, olHTML
  56.  
  57.     If msg.Attachments.Count > 0 Then
  58.         For Each att In msg.Attachments
  59.             attpath = folder + CStr(msgday) + "_" + att.FileName
  60.             att.SaveAsFile attpath
  61.         Next
  62.     End If
  63.    
  64. Next
  65.  
  66. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement