`
jason_zh
  • 浏览: 8259 次
  • 性别: Icon_minigender_1
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

Export the Outlook messages in txt format

VB 
阅读更多
**************************************************
' Title: PST2TXT
'
' Description:
' This VB application will export selected
' Outlook folders to file system as TXT files.
' The intent is to allow quick reference when
' burned to CD due to Outlook not opening
' Read Only PST files.
'
' Use: Paste the code into a VB5/6 module
' There is an optional Form explained in code
'
' Notes:
' This code is offered 'As Is'.
' No support will be provided by me.
'
' Author: Steven Harvey
' Free to use for all
'
'**************************************************
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const MAX_PATH = 260

Public Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
End Type

'APIs for the folder selection
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

Private objNS As NameSpace
Private objFolder As Outlook.MAPIFolder
Private strDestination As String
Private strTopFolder As String
Private strLogFile As String
Private intErrors As Boolean
Public intUserAbort As Integer

Sub Main()
  Set objNS = Application.GetNamespace("MAPI")
  Set objFolder = objNS.PickFolder
 
  If Not objFolder Is Nothing Then
    strTopFolder = CleanString(objFolder.Name)
    strDestination = GetFileDir
    If strDestination <> "" Or strDestination <> Null Then
      strFolderName = CleanString(objFolder.Name)
      strLogFile = strDestination & "\" & strFolderName & "\ExportLog.txt"
      strDestination = strDestination & "\" & strFolderName
      If FolderExist(strDestination) Then
        MsgBox "This folder has already been exported here. Please clear the destination or choose another."
        Exit Sub
      Else
        '****** frmProcessing displays while processing messages.
        '****** It has a message asking user to wait while processing.
        '****** It also has a cancel button to set intUserAbort to 1.
        '****** Form's button code is below
        '*** Private Sub cmdCancel_Click()
        '***   intUserAbort = 1
        '***   Unload Me
        '*** End Sub
        'frmProcessing.Show
       
        intUserAbort = 0
        ProcessFolder objFolder, strDestination
       
        'Unload frmProcessing
       
        If intUserAbort = 0 Then
            MsgBox "Export Complete!" & vbCrLf & "Export log file location:" & vbCrLf & strLogFile
        Else
            MsgBox "Processing cancelled." & vbCrLf & "Export log file location:" & vbCrLf & strLogFile
        End If
      End If
    Else
      MsgBox "Destination folder selection cancelled!"
    End If
  Else
    MsgBox "MAPI folder selection cancelled!"
  End If

Set objNS = Nothing
Set objFolder = Nothing
End Sub

Function FolderExist(sFileName As String) As Boolean
  FolderExist = IIf(Dir(sFileName, vbDirectory) <> "", True, False)
End Function

Public Function StripNulls(ByVal OriginalStr As String) As String
    If (InStr(OriginalStr, Chr$(0)) > 0) Then
        OriginalStr = Left$(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function

Public Function GetFileDir() As String
Dim ret As String
    Dim lpIDList As Long
    Dim sPath As String, udtBI As BrowseInfo
    Dim RdStrings() As String, nNewFiles As Long

    'Show a browse-for-folder form:
    With udtBI
        .lpszTitle = lstrcat("Please select a folder to export to:", "")
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList = 0 Then Exit Function
       
    'Get the selected folder.
    sPath = String$(MAX_PATH, 0)
    SHGetPathFromIDList lpIDList, sPath
    CoTaskMemFree lpIDList
    sPath = StripNulls(sPath)
    GetFileDir = sPath
End Function

Sub ProcessFolder(StartFolder As Outlook.MAPIFolder, strPath As String)
    Dim objItem As Object
       
    MkDir strPath
    ' process all the items in this folder
    For Each objItem In StartFolder.Items
      DoEvents
      SaveAsMsg objItem, strPath
      Set objItem = Nothing
    Next
   
    ' process all the subfolders of this folder
    For Each objFolder In StartFolder.Folders
        Dim strSubFolder As String
        strSubFolder = strPath & "\" & CleanString(StartFolder.Name)
        Call ProcessFolder(objFolder, strSubFolder)
    Next
   
    Set objFolder = Nothing
    Set objItem = Nothing
End Sub

Private Function CleanString(strData As String) As String
    'Replace invalid strings.
    strData = ReplaceChar(strData, "_", "")
    strData = ReplaceChar(strData, "L", "'")
    strData = ReplaceChar(strData, "`", "'")
    strData = ReplaceChar(strData, "{", "(")
    strData = ReplaceChar(strData, "[", "(")
    strData = ReplaceChar(strData, "]", ")")
    strData = ReplaceChar(strData, "}", ")")
    strData = ReplaceChar(strData, "/", "-")
    strData = ReplaceChar(strData, "\", "-")
    strData = ReplaceChar(strData, ":", "")
    strData = ReplaceChar(strData, ",", "")
    'Cut out invalid signs.
    strData = ReplaceChar(strData, "*", "'")
    strData = ReplaceChar(strData, "?", "")
    strData = ReplaceChar(strData, """", "'")
    strData = ReplaceChar(strData, "<", "")
    strData = ReplaceChar(strData, ">", "")
    strData = ReplaceChar(strData, "|", "")
    CleanString = Trim(strData)
End Function

Private Function ReplaceChar(strData As String, strBadChar As String, strGoodChar As String) As String
Dim tmpChar, tmpString As String
    For i = 1 To Len(strData)
      tmpChar = Mid(strData, i, 1)
      If tmpChar = strBadChar Then
        tmpString = tmpString & strGoodChar
      Else
        tmpString = tmpString & tmpChar
      End If
    Next i
    ReplaceChar = Trim(tmpString)
End Function

Private Sub SaveAsMsg(objItem As Object, strFolderPath As String)
On Error Resume Next
Dim strSubject As String
Dim strSaveName As String

    Err.Clear
    If Not objItem Is Nothing Then
      Select Case TypeName(objItem)
        Case "AppointmentItem"
          strSaveName = Format(objItem.Start, "mm-dd-yyyy hh.nn.ss") & " " & IIf(Len(strFolderPath & objItem.Subject) > 255, Left(objItem.Subject, 255 - Len(strFolderPath)), objItem.Subject)
        Case "MailItem"
          strSaveName = Format(objItem.ReceivedTime, "mm-dd-yyyy hh.nn.ss") & " " & IIf(Len(strFolderPath & objItem.Subject) > 255, Left(objItem.Subject, 255 - Len(strFolderPath)), objItem.Subject)
          If Err Then
              WriteToLog "Error #" & Err.Number & ": " & Err.Description & " Unable to process message '" & strFolderPath & "\" & objItem.Subject & "'."
              strSaveName = strFolderPath & "\" & objItem.Subject
          End If
        Case "NoteItem"
          strSaveName = objItem.Subject
        Case "TaskItem"
          strSaveName = objItem.Subject
        Case "ContactItem"
          strSaveName = objItem.FileAs
        Case Else
          strSaveName = ""
      End Select
        Err.Clear
        objItem.SaveAs strFolderPath & "\" & CleanString(strSaveName) & ".txt", olTXT
        If Err Then
            WriteToLog "Error #" & Err.Number & ": " & Err.Description & " Unable to process message '" & strFolderPath & "\" & objItem.Subject & "'."
        Else
          WriteToLog "Success: " & strFolderPath & "\" & CleanString(strSaveName)
        End If
    End If
End Sub

Private Sub WriteToLog(strMessage As String)
  intErrors = True
  Open strLogFile For Append As #1
  Write #1, strMessage
  Close #1
End Sub
分享到:
评论

相关推荐

    OutlookAttachView v2.73

    OutlookAttachView scans all messages stored in your Outlook, and displays the list of all attached files that it finds. You can easily select one or more attachments and save all of them into the ...

    EMS Data Export 4.6.0.5

    There will be no need to waste your time on tiresome data conversion - Advanced Data Export will do the task quickly and produce the result in the desired format. Visit our web-site for details: ...

    NetFlow Export Datagram Format

    NetFlow是网络流量分析工具,由思科公司开发,用于收集网络中数据包的统计信息,帮助网络管理员了解流量模式和网络使用情况。NetFlow导出的数据报格式是用于在路由器和交换机上收集和导出网络流量数据的标准格式,...

    EMS Advanced Data Export Component Suite v4.13.3.1.D5-XE10.1.Src

    EMS Advanced Data Export VCL v4.13.... There will be no need to waste your time on tiresome data conversion - Advanced Data Export will do the task quickly and will give the result in the desired format.

    Export_Class_in_the_format_of_DLL.rar_The Class

    - 导出类:使用`__declspec(dllexport)`关键字标记要导出的类。在DLL的头文件中,这个关键字告诉编译器这个类需要被其他模块使用。 - 导入类:在使用DLL的客户端代码中,使用`__declspec(dllimport)`导入所需的类...

    EMS Advanced Data Export VCL v4.9.0.1 for D5-XE5 Full Source

    来自俄罗斯网站的好东东 ... There will be no need to waste your time on tiresome data conversion - Advanced Data Export will do the task quickly and will give the result in the desired format.

    Advanced Data Export VCL 4.14.1.5 Full Source

    There will be no need to waste your time on tiresome data conversion - Advanced Data Export will do the task quickly and will give the result in the desired format. Key Features Data export into 17 ...

    EMS Advanced Data Export VCL v4.9.0.1 Full Source

    There will be no need to waste your time on tiresome data conversion - Advanced Data Export will do the task quickly and will give the result in the desired format. Key Features Data export into 17 ...

    EMS.Advanced.Data.Export.VCL.v4.9.0.1.Full.Source

    If format masks correspond to system masks, they were not saved in the dfm file. Fixed now. 9. When removing a delimiter in the Formats property, it was replaced by #0 in the corresponding format ...

    Oracle9i Database Error Messages

    Recognizing Variable Text in Messages Message Stacks Contacting Oracle Support Services Oracle Exception Messages Trace Files The Alert File Part II Oracle Database Server Messages 2 ORA-00000 to ORA-...

    tableExport_html表格导出excel等多种格式

    3. **自定义设置**:tableExport提供了丰富的配置选项,可以定制导出时的样式、分隔符、编码等参数,以满足特定的格式要求或者提高导出数据的可读性。 4. **兼容性**:由于是基于JavaScript实现,该工具具有良好的...

    pandoc export failed

    然而,有时在使用Pandoc进行导出时,可能会遇到“pandoc export failed”的错误提示,这可能会对我们的工作流程造成困扰。本文将深入探讨这个问题,分析其可能的原因,并提供相应的解决策略。 首先,我们要理解...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    - fixed output of the check boxes on the highlighted lines in PDF export - fixed bug with PDF anchors - fixed bug when using two or more macroses in memo version 4.12 --------------- + added support ...

    exportlist.txt

    exportlist.txt

    Adcanced Data Export VCL 4.9.0.1(D5-XE4)

    Data export into 17 most popular formats: MS Access, MS Excel, MS Word, Open XML Format, Open Document Format (ODF), RTF, HTML, XML, PDF, TXT, DBF, CSV, SYLK, DIF, LaTeX, SQL and Windows Clipboard ...

    export_fig.rar_export_fig_fig

    在 MATLAB 环境中,`export_fig` 是一个非常实用的工具,它允许用户将图形以高质量的格式导出,适用于出版、报告或者网页展示。这个“export_fig.rar_export_fig_fig”压缩包可能包含了 `export_fig` 工具以及一个...

    exportfig.rar_export fig_in

    标题“exportfig.rar_export fig_in”暗示我们讨论的是关于如何在MATLAB中使用`export_fig`命令将图形保存到外部文件的过程。`export_fig`的一个关键优点是它可以保留图形的所有细节,包括透明度、线条宽度、字体...

    SIMATIC-SCADA-Export-V18安装包-链接地址.txt

    SIMATIC_SCADA_Export_V18安装包

    SIMATIC-SCADA-Export-V17安装包-链接地址.txt

    SIMATIC-SCADA-Export-V17安装包

Global site tag (gtag.js) - Google Analytics