Consulting

Results 1 to 3 of 3

Thread: Save Each Email In Folder As Text File

  1. #1
    VBAX Newbie
    Joined
    Oct 2011
    Posts
    2
    Location

    Save Each Email In Folder As Text File

    Hi All,

    Using Outlook 2010.

    I would like to loop through a folder in .pst and save each mailitem as a text file.

    When I run my code, I receive this error message:
    Run-time error "-2147221233 (8004010f)':
    The attempted opderation failed. An Object could not be found.
    Debug points here
    Set olFolderPayments = olNS.Folders("pst-goss\payments") '<- \\ Removed
    The path and folder definitely exist.
    I ran some code to print the folder name and path tro the immediate window and that worked great so I am not sure what I'm doing wrong?

    Edit: The paths in the code below are correct. I was forced to remove them before I could post

    Thanks,
    goss

    Full code:
    Option Explicit
    Sub SavePmntsAsTxt()
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Author: Unknown (MSDN)
        'Date Extracted: 10/08/2011
        'Link: (Removed)
        '&
        'Link: (Removed)
        'Modified by: goss
        'Modified Date: 10/08/2011
        'References: Microsoft Scripting Runtime
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     
        Dim olApp As Application
        Dim olNS As NameSpace
        Dim olFolderPayments As MAPIFolder
        Dim item As Object
        Dim strReports As String
        Dim strFile As String
        Dim strBody As String
     
        Set olApp = New Outlook.Application
        Set olNS = olApp.GetNamespace("MAPI")  ' open the MAPI Namespace
        Set olFolderPayments = olNS.Folders("") '<-Removed
        strReports = "C:\Payments\"
     
        With olFolderPayments
            For Each item In .Items
                strBody = item.Body
                strFile = strReports & "Payment_" & Format(item.ReceivedTime, "yyyymmdd_hhmmss") & ".txt"
                Open strFile For Output As #1
                Print #1, strBody
                Close #1
            Next item
        End With
     
        'Tidy up
            Set olApp = Nothing
            Set olNS = Nothing
            Set olFolderPayments = Nothing
            Set olFolderxPayments = Nothing
            Set item = Nothing
    End Sub

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    You can't specify more than one folder level using the Folders object. You have to walk the hierarchy and set references as you go. For example if your target folder was Inbox\My Folders\My Other Folder you would need three references (or one reference, adjusted three times).
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  3. #3
    VBAX Newbie
    Joined
    Oct 2011
    Posts
    2
    Location
    Thanks JP,

    My apologies, I'm not seeing it.
    I found the snippet below, ran it and it works.
    So I lifted some pieces from this snippet to create my near-final code

    I guess I'm not totally sure why this works and mine does not?

    Thanks
    g

    Option Explicit
    Dim olApp As Outlook.Application
    Public Sub GetMail()
     
    ' Reference: Microsoft Outlook Object Library
     
      Dim olNS As NameSpace
      Dim olFolder As MAPIFolder
     
      Set olApp = New Outlook.Application
      Set olNS = olApp.GetNamespace("MAPI")  ' open the MAPI Namespace
     
      Dim fol As MAPIFolder
      Dim subFolder As MAPIFolder
      Dim subsubfolder As MAPIFolder
     
      Dim strEntryID As String
     
      For Each fol In olNS.Folders
        Debug.Print fol.Name, fol.FolderPath
        For Each subFolder In fol.Folders
          Debug.Print "  -" & subFolder.Name, subFolder.FolderPath, subFolder.EntryID
    '      If subFolder.FolderPath = [link removed] Then: strEntryID = subFolder.EntryID
    '      For Each subsubfolder In subFolder.Folders
    '        If subsubfolder.FolderPath = [link removed] Then: strEntryID = subsubfolder.EntryID
    '        Debug.Print "    -" & subsubfolder.Name, subsubfolder.FolderPath, subsubfolder.EntryID
    '      Next
        Next
      Next
    '  If strEntryID = "" Then
    '    MsgBox "lifeonline not found"
    '    Exit Sub
    '  Else
    '    Set olFolder = olNS.GetFolderFromID(strEntryID)
    '  End If
    End Sub

Posting Permissions

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