Exporting Outlook PST files to flat/text files? - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Exporting Outlook PST files to flat/text files?

  1. #11
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    Originally posted here by ric-o
    I'm not in a rush...got some time. Wow, that would be cool HT! Thanks.
    Not a problem... this is a little new to me... otherwise it'd be done.. (maybe.. heh)... I'm gonna see what I can do anyways...
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  2. #12
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    Originally posted here by HTRegz
    Not a problem... this is a little new to me... otherwise it'd be done.. (maybe.. heh)... I'm gonna see what I can do anyways...
    Hey Hey,

    So I put in a bit of time while waiting for a phone call..

    This will parse your Inbox in Outlook... and dump everything to a single text file in the root of c:

    Code:
    import win32com.client
    outlook = win32com.client.Dispatch("Outlook.Application")
    namespace = outlook.GetNamespace("MAPI")
    inbox = namespace.GetDefaultFolder(6).Items
    for item in inbox:
        msgFrom = "From: " + str(item.sendername) + " (" + str(item.senderemailaddress) + "}"
        msgTo = "To: " + str(item.to)
        msgDate = "Date: " + str(item.ReceivedTime)
        msgSubject = "Subject: " + str(item.subject)
        msgBody = "Message: \n\n" + item.body.encode( "utf-8" )
        fp = open("c:\outlookMessages.txt", "a")
        fp.write("--- Start Message ---")
        fp.write("\n")
        fp.write(msgFrom)
        fp.write("\n")
        fp.write(msgTo)
        fp.write("\n")
        fp.write(msgDate)
        fp.write("\n")
        fp.write(msgSubject)
        fp.write("\n")
        fp.write(msgBody)
        fp.write("\n")
        fp.write("--- End Message ---")
        fp.write("\n\n")
        fp.close()
    It requires Pythonwin from ActiveState for the win32com.client file...

    I'm working on parsing pst files themselves for this.. but I've hit a problem with namespace.addstore() but I'll see what I can do..

    Peace,
    HT
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  3. #13
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Excellent work HTRegz. I was actually inspired by your comments, and
    wrote a little VBA-Script, which allows for an easy automation of
    the task without external tools.


    The following VBA-macro (runs in Outlook) stores each message as a
    "datetime_from.txt"-file in the folder c:\test, and stores its attachments in
    the folder c:\test\att.
    It does not handle multiple folders with mail-items; it just uses the default inbox.
    This could/should be extended, and I could do it for you, ric-o, if needed...
    Note, that maybe, one has to change the security level[1], except one knows how
    to sign the code.


    How does the automation work:

    Part A: create the basis

    1. create a directory c:\test and c:\test\att
    2. create an outlook profile "export", configured for an outlook-pst file c:\test\temp.pst
    3. make sure that outlook opens automatically c:\test\temp.pst when starting outlook ("export" as default profile)
    4. in outlook: open the VBA-Editor by pressing Alt-F11
    5. add the code below in the existing module or create a new one
    6. save and test it in the VBA-Editor: it should export the Inbox-Folder and close Outlook
    7. start outlook again. the macro now should be available in the macro-list in Outlook (Alt-F8)
    8. exit outlook and start it once again by using the command line parameter
    " ...outlook.exe /autorun Export_Inbox" (works only in Outlook 2003). At startup, outlook
    will run the Macro "Export_Inbox" (which then closes outlook).
    9. remove the directory c:\test

    Part B: do the loop

    Create a batch-file that does the following:
    a) create the c:\test and c:\test\att-folders
    b) copy "pst_1.pst" (or whatever) to c:\test\temp.pst
    c) start outlook as above with the /autorun-option: -> the export is running
    d) delete the file c:\test\temp.pst
    e) rename the test-folder or move it to wherever you want
    f) loop to a) and copy the next pst-file.


    Code:
    Sub Export_Inbox()
    
     On Error GoTo ErrorOcccured
     
     Dim ns As namespace
     Dim inbox As MAPIFolder
     Dim item As Object
     Dim att As Attachment
     
     Dim mail As String
     Dim filename As String
    
     ' open Default Inbox
     Set ns = GetNamespace("MAPI")
     Set inbox = ns.GetDefaultFolder(olFolderInbox)
     
     If inbox.Items.Count = 0 Then
         Exit Sub
     End If
    
     ' loop the items (emails)
     For Each item In inbox.Items
        
        mail = "From: " & item.SenderName & " (" & item.SenderEmailAddress & ") " & vbCrLf
        mail = mail & "To: " & item.To & vbCrLf
        mail = mail & "Cc: " & item.CC & vbCrLf
        mail = mail & "Date: " & item.ReceivedTime & vbCrLf
        mail = mail & "Subject: " & item.Subject & vbCrLf
        mail = mail & "Attachments: "
       
        ' loop the attachments
        For Each att In item.Attachments
           filename = "C:\test\att\" & att.filename
           att.SaveAsFile filename
           mail = mail & att.filename & vbTab
        Next att
        mail = mail & vbCrLf & "Message: " & vbCrLf & vbCrLf & item.Body & vbCrLf
        
        filename = "c:\test\" & Format(item.ReceivedTime, "ddmmyyyyhhmmss") & "_" & item.SenderName & ".txt"
        Open filename For Append Access Write As #1
        Print #1, mail
        Close #1
     
     Next item
     
    Destructor:
       Set att = Nothing
       Set item = Nothing
       Set ns = Nothing
     
       ' exit Outlook to allow for automation
       Set MyOutlook = CreateObject("Outlook.Application")
       MyOutlook.Quit
       Set MyOutlook = Nothing
         
       Exit Sub
    
    ErrorOcccured:
       MsgBox (Err.Number & vbCrLf & Err.Description)
       Resume Destructor
    End Sub
    Cheers

    [1] http://office.microsoft.com/en-gb/as...850551033.aspx
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  4. #14
    Member
    Join Date
    Sep 2005
    Posts
    77
    I have used a program before called Dawn. Its freeware. It has a ton of options that I have not had the need for, maybe it might be of use for you:

    "Dawn converts Address Books among various Address Book Programs.
    Dawn is equipped with an extraction feature. It is absolutely free to use
    and distribute in the form seen here, but the source code is not freely
    distributable.

    New in this release: vCard (VCF) support, Outlook 2003 support, Many new fields supported, Bug fixes for the CSV format, Opera, Mozilla, Netscape, and others

    Dawn works with the following programs:

    Becky!, Pine (for PC and UNIX), Mozilla, Netscape 6/7, Netscape Communicator 4.x, Netscape Navigator 3.x, Opera, Outlook Express / Windows Address Book (WAB), Outlook 98/2000/2002/2003, Palm Desktop, Simeon, ExecMail, Juno, Corel WordPerfect Address Book 8.x, Eudora 3.x, 4.x, 5.x, 6.x, Pegasus Mail 3.x and 4.x, Other programs which can use/import/export LDAP/LDIF, Comma Separated Value (CSV), vCard (VCF), plain text, one per line or comma separated formats or store addresses in a text file"
    %42%75%75%75%75%72%70%21%00

  5. #15
    Senior Member Spyrus's Avatar
    Join Date
    Oct 2002
    Posts
    742
    There was also this: http://www.processtext.com/abcoutlk.html

    But I think you have plenty of good options now
    Duct tape.....A whole lot of Duct Tape
    Spyware/Adaware problem click
    here

  6. #16
    oldie ric-o's Avatar
    Join Date
    Nov 2002
    Posts
    487
    HT, sec_ware - you guys ROCK! Thanks a ton for the help! I'm fooling around with them now. Will let you know if any questions or need more help. (AO aint letting me give you peeps any greenies at this point..sorry).

    Nihil, Spyrus, Eyecre8, thanks to you for those apps too.

  7. #17
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    ric-o and me, we discussed the issue during the past few days, and performed
    a few tests. Our script now is capable to extract all emails (and attachments) from
    all email-folders stored in a pst-file. The content of an email-folder is stored
    in a separate file ( la HTRegz).

    In order to allow for automation, Outlook may have to be configured not to show the
    reminders (Options.Advanced.Reminder Options). Note that Outlook 2003 is needed
    for the /autrun-option.

    We hope that this may help others as well.

    Cheers


    Code:
    
    ' Idea of the recursive handling taken from Michael Bauer[1].  
    Sub HandleAllItems(oRoot As Outlook.namespace, _
      ByVal bRecursive As Boolean _
    )
      LoopFolders oRoot.Folders, bRecursive
    End Sub
    
    
    Sub LoopFolders(oFolders As Outlook.Folders, ByVal bRecursive As Boolean)
      Dim oFld As Outlook.MAPIFolder
      
      ' Loop through all folders.
      For Each oFld In oFolders
        ' Loop through folder items
        LoopItems oFld.Items, oFld.Name
        
        If bRecursive Then
          ' Call this function again for going _
            deeper into the object hierarchy.
          LoopFolders oFld.Folders, bRecursive
        End If
      Next
    End Sub
    
    Sub LoopItems(oItems As Outlook.Items, FolderName As String)
      Dim obj As Object
      
      For Each obj In oItems
        Select Case True
        Case TypeOf obj Is Outlook.MailItem
          HandleMailItem obj, FolderName
        End Select
      Next
    End Sub
    
    Sub HandleMailItem(oItem As Outlook.MailItem, FolderName As String)
        Dim mail As String
        Dim filename As String
        
        mail = "--- Begin Message ---" & vbCrLf
        mail = mail & "From: " & oItem.SenderName & " (" & oItemSenderEmailAddress & ") " & vbCrLf
        mail = mail & "To: " & oItem.To & vbCrLf
        mail = mail & "Cc: " & oItem.CC & vbCrLf
        mail = mail & "Date: " & oItem.ReceivedTime & vbCrLf
        mail = mail & "Subject: " & oItem.Subject & vbCrLf
        mail = mail & "Attachments: "
       
        ' loop the attachments
        For Each att In oItem.Attachments
           filename = "C:\test\att\" & att.filename
           att.SaveAsFile filename
           mail = mail & att.filename & vbTab
        Next att
        mail = mail & vbCrLf & "Message: " & vbCrLf & vbCrLf & oItem.Body & vbCrLf
        mail = mail & "--- End Message ---" & vbCrLf
     
        filename = "c:\test\" & FolderName & ".txt"
        Open filename For Append Access Write As #1
        Print #1, mail
        Close #1
    End Sub
    
    
    
    Sub Export_Inbox()
      
     On Error GoTo ErrorOcccured
     
     Dim ns As namespace
     Set ns = GetNamespace("MAPI")
     
     HandleAllItems ns, True
     
    Destructor:
        Set ns = Nothing
    
       ' exit Outlook to allow for automation
       Set MyOutlook = CreateObject("Outlook.Application")
       MyOutlook.Quit
       Set MyOutlook = Nothing
       
        Exit Sub
        
    ErrorOcccured:
       MsgBox (Err.Number & vbCrLf & Err.Description)
       Resume Destructor
    End Sub

    [1] http://www.office-loesung.de/ftopic36102_0_0_asc.php
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  8. #18
    Junior Member
    Join Date
    Apr 2006
    Posts
    20
    If you want to use Perl, take a look at Mail::Outlook from CPAN.

    Here is part of the example crom CPAN:
    use Mail::Outlook;
    my $outlook = new Mail::Outlook();

    # start with a folder
    my $outlook = new Mail::Outlook('Inbox');

    # use the Win32::OLE::Const definitions
    use Mail::Outlook;
    use Win32::OLE::Const 'Microsoft Outlook';
    my $outlook = new Mail::Outlook(olInbox);

    # get/set the current folder
    my $folder = $outlook->folder();
    my $folder = $outlook->folder('Inbox');

    # get the first/last/next/previous message
    my $message = $folder->first();
    $message = $folder->next();
    $message = $folder->last();
    $message = $folder->previous();

    # read the attributes of the current message
    my $text = $message->From();
    $text = $message->To();
    $text = $message->Cc();
    $text = $message->Bcc();
    $text = $message->Subject();
    $text = $message->Body();

    -GA
    Jive Lady: Jus\' hang loose, blood. She gonna catch ya up on da\' rebound on da\' med side.
    Second Jive Dude: What it is, big mama? My mama no raise no dummies. I dug her rap!
    Jive Lady: Cut me some slack, Jack! Chump don\' want no help, chump don\'t GET da\' help! Jive ass dude don\'t got no brains anyhow! Hmmph!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides