VB6 - Export emails from Outlook PST file to RFC822 message files (*.eml)

The following VB.NET codes demonstrates how to export emails from Outlook INBOX/PST file to RFC822 message files. It requires Outlook is installed on current machine.

Installation

Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first. Full sample projects are included in this installer.

Add reference

To use EAGetMail POP3 & IMAP4 ActiveX Object in your project, the first step is Add reference of EAGetMail to your project. Please go to menu -> Project -> References -> and select EAGetMailObj ActiveX Object, click OK, the reference will be added to your project, and you can start to use it to retrieve email and parse email in your project.

add reference in VB6

Add reference in VBA (EXCEL/Access/Outlook)

Open VBA IDE by press Alt+F11, Please select menu -> Tools -> References -> and select EASendMailObj ActiveX Object, click OK, the reference will be added to current VBA project, and you can start to use it to retrieve email and parse email in your VBA project.

To better demonstrate how to retrieve email and parse email, let’s create a VB 6.0 Standard EXE project at first, then add a CommandButton on the Form, double-click this button. It is like this

VB 6.0 standard project

VB - Export emails from Outlook

The following example codes demonstrate exporting emails from Outlook INBOX/PST file to RFC822 message files

Note

To get full sample projects, please download and install EAGetMail on your machine.

' Export emails from Outlook folder/PST file to rfc822 message files.
Sub ExportMailItemToRfc822(ByVal destFolder)

    Dim app
    Set app = CreateObject("Outlook.Application")

    Const olFolderInbox = 6
    Const olDiscard = 1
    Const olMSGUnicode = 9

    Dim folder
    Set folder = app.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

    ' If you want to export emails from
    ' existed PST, you can use the following codes.
    ' app.Session.AddStore("d:\tempFolder\test.pst")
    ' Set folder = app.GetNamespace("MAPI").Folders.GetLast()

    Dim oTool
    Set oTool = CreateObject("EAGetMailObj.Tools")

    Dim items
    Set items = folder.items

    Dim i
    For i = 1 To items.Count
        Dim mailItem
        Set mailItem = items(i)

        Dim tempMsgFile
        tempMsgFile = destFolder & "\temp_" & i & ".msg"
        mailItem.SaveAs tempMsgFile, olMSGUnicode

        Dim mail
        Set mail = CreateObject("EAGetMailObj.Mail")
        mail.LicenseCode = "TryIt"

        mail.LoadOMSGFile tempMsgFile
        mail.Update

        Dim rfc822MessageFile
        rfc822MessageFile = destFolder & "\rfc822_" & i & ".eml"

        mail.SaveAs rfc822MessageFile, True

        oTool.RemoveFile(tempMsgFile)

    Next

    app.Quit
End Sub

Private Sub Command1_Click()

On Error GoTo ErrorHandle
    ExportMailItemToRfc822 "D:\tempfolder"

    Exit Sub
ErrorHandle:
    MsgBox Err.Description
End Sub

32bit/x64 ActiveX DLL

Seperate builds of run-time dll for 32 and x64 platform

File Platform
Installation Path\Lib\native\x86\EAGetMailObj.dll 32 bit
Installation Path\Lib\native\x64\EAGetMailObj.dll 64 bit

Distribution

  • Standard EXE

    For VB6, C++, Delphi or other standard exe application, you can distribute EAGetMailObj.dll with your application to target machine without COM-registration and installer. To learn more detail, please have a look at Registration-free COM with Manifest File.

  • Script

    For ASP, VBScript, VBA, MS SQL Stored Procedure, you need to install EAGetMail on target machine by EAGetMail installer, both 32bit/x64 DLL are installed and registered.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.