StartDownloadsServiceSamplesWorkshopsContact DeutschEnglish
 
Samples
General
Outlook®
 
Awarded by
Microsoft since 2005:
mvp logo
VBOffice Info
Visitors1797195
Impressions6733057
Links
Imprint
Privacy Policy
Contact
E-Mail: Save new items immediately as files
Author: Michael BauerHomepage
Date: 19.01.2006Accessed: 49903
  
Description

The sample saves incoming emails as *.msg files into the file system. The email's subject and received time will be its file name. Please note that in this sample the directory in MAIL_PATTH must exist already!

Option Explicit

Public Enum olSaveAsTypeEnum
  olSaveAsTxt = 0
  olSaveAsRTF = 1
  olSaveAsMsg = 3
End Enum

Private WithEvents Items As Outlook.Items

Private Const MAIL_PATH As String = "d:\mails\"

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
    SaveMailAsFile Item, olSaveAsMsg, MAIL_PATH
  End If
End Sub

Private Sub SaveMailAsFile(oMail As Outlook.MailItem, _
  eType As olSaveAsTypeEnum, _
  sPath As String _
)
  Dim dtDate As Date
  Dim sName As String
  Dim sFile As String
  Dim sExt As String

  Select Case eType
    Case olSaveAsTxt: sExt = ".txt"
    Case olSaveAsMsg: sExt = ".msg"
    Case olSaveAsRTF: sExt = ".rtf"
    Case Else: Exit Sub
  End Select

  sName = oMail.Subject
  ReplaceCharsForFileName sName, "_"

  dtDate = oMail.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & sExt

  oMail.SaveAs sPath & sName, eType
End Sub

Private Sub ReplaceCharsForFileName(sName As String, _
  sChr As String _
)
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
End Sub
 
 

ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the ... [more]

 

Access the master category list in the blink of an eye, share your categories in a network, get a reminder service, and ... [more]

 

SAM automatically sets the sender, signature, and folder for sent items, for instance based on the recipient ... [more]

 

OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or ... [more]