none
Macro to pull body text of all messages in a public folder RRS feed

  • Question

  • I need to pull all of the body text from a public folder and get it into a document somehow. Copy and paste, write to a text file, whatever. Here is what I have so far, but I can't seem to get the body text, any ideas?

        Dim oMails As Outlook.Items
        Dim oMailItem As Outlook.MailItem
        Dim oProp As Outlook.PropertyPage
        Dim sSubject As String
        Dim sBody
        
        Dim nw As Outlook.NameSpace
        Set nw = Application.GetNamespace("MAPI")
        Dim folder As folder
        Set folder = nw.Folders("Public Folders - user@domain.com")
        Dim nextFolder As folder
        Set nextFolder = folder.Folders("All Public Folders")
        Dim thirdFolder As folder
        Set thirdFolder = folder.Folders("CNB Community Bank")
        Set oMails = thirdFolder.Items

        For Each oMailItem In oMails
            sBody = oMailItem.Body
        Next


    - Michael

    Monday, June 4, 2012 9:40 PM

Answers

  • Something along the lines:

    Const fsoForAppend = 8
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objTextStream
    Set objTextStream = objFSO.OpenTextFile("C:\test.txt", fsoForAppend)
    objTextStream.WriteLine sBody
    objTextStream.Close
    Set objTextStream = Nothing
    Set objFSO = Nothing


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, June 5, 2012 7:38 PM

All replies

  • Your code keeps overwriting the same variable (sBody).

    Either append to the variable (sBody = sBody + oMailItem.Body) or use the FileSystemObject to write the data to the file system.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!


    Monday, June 4, 2012 10:02 PM
  • I should have been more clear, I know that the code would not pull all the bodies as is, but it wasn't pulling anything. I found I had a bug in getting to the right folder. I'm getting all of the content now. How would I go about either outputting this, or saving it to a file?

    *edit

    I got what I needed

    Open "C:\messages.txt" For Append As 1

    For Each oMailItem In oMails

    sBody = oMailItem.Body

    Print #1, sBody

    Next

    Close #1


    - Michael


    Tuesday, June 5, 2012 1:21 PM
  • Something along the lines:

    Const fsoForAppend = 8
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objTextStream
    Set objTextStream = objFSO.OpenTextFile("C:\test.txt", fsoForAppend)
    objTextStream.WriteLine sBody
    objTextStream.Close
    Set objTextStream = Nothing
    Set objFSO = Nothing


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Tuesday, June 5, 2012 7:38 PM