how to combine multiple message texts into one text file

Posted on 2005-05-12
Last Modified: 2010-04-08
I have several thousand messages, each consisting of a single line of text... is there any good way to take the one line of text from each of these messages and combine them into a single text file?
Question by:jrcooperjr
    LVL 76

    Expert Comment

    by:David Lee
    Hi jrcooperjr,

    Are the message alone in one folder or could they all be moved into one folder?  If so, then I can wput together a simple macro that'll go through them all and write them out into one text file.

    LVL 1

    Author Comment

    They are all in one folder.....
    LVL 76

    Accepted Solution


    Here it is.  This macro will loop through all the messages in a given folder and write the contents of each message's body to a file, one line per file.  Of course if a message actually has more than one line in it, then this approach will write them all to the text file.  I wrote and tested this using Outlook 2002 and it worked perfectly.  Here's what you'll need to do to use this.
    1.  Start Outlook
    2.  Click Tools->Macro->Visual Basic Editor
    3.  If not already expanded, expand Modules and click on Module1
    4.  Copy the code below and paste it into the right-hand pane of the VB editor window
    5.  Edit the code as needed
    6.  Click the diskette icon on the toolbar to save changes
    7.  Close the VB Editor
    8.  Click Tools->Macro->Security
    9.  Set the Security Level to Medium
    10.  Expand your folder list and click on the folder containing the messages
    11.  Run the macro by clicking Tools->Macro->Macros->WriteTextToFile->Run
    12.  When the "All done" message appears, go check the file and it should contain the contents of all the messages from that folder

    Sub WriteTextToFile()
        Dim olRootFolder As MAPIFolder, _
            olExplorer As Outlook.Explorer, _
            olMessage As Outlook.MailItem, _
            objFSO As Object, _
            objFile As Object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Change the filename and path as desired
        Set objFile = objFSO.CreateTextFile("C:\eeTesting\Q_21422190.Txt")
        Set olExplorer = Application.ActiveExplorer
        Set olRootFolder = olExplorer.CurrentFolder
        For Each olMessage In olRootFolder.Items
            objFile.WriteLine olMessage.Body
        Set objFile = Nothing
        Set olMessage = Nothing
        Set olRootFolder = Nothing
        Set olExplorer = Nothing
        Set objFSO = Nothing
        MsgBox "All done."
    End Sub
    LVL 1

    Author Comment

    Thanks very much! Your code did exactly what I needed...
    I'm also studying VB scripting, so your example helped me out there too...
    LVL 76

    Expert Comment

    by:David Lee
    No problem.  Glad I could help out.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
    Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now