+ Reply to Thread
Results 1 to 2 of 2

Converting MSG files to TXT files

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2018
    Location
    Cleveland, Ohio
    MS-Off Ver
    2010
    Posts
    2

    Converting MSG files to TXT files

    Hello,

    I am probably posting a question that has already been answered. Because I am new to the Excel Forum, I was unable to find the solution.

    I am writing a macro that includes converting a series to msg files to corresponding txt files. The full path is the parameter, for example "c:\Documents\OutlookFiles\message1.msg" Has this VBA code been written before? Thanks!

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Converting MSG files to TXT files

    Hi,
    Basically you can copy/paste all code below in a VBA module, then add Reference to "Microsoft Outlook Objects Library".
    Change the path to the folder where all yours .msg file are (under ConvertMsgToTXT sub, see comment in code).
    What you should be aware of - this code creates separate .txt file for each .msg, name of each .txt file is "Sender" + "Sent Date". In case there will be more than one Outlook message from the same Sender - files fill be re-written.
    You should run "Sub ConvertMsgToTXT()" to get all .msg files converted.


    Sub ConvertMsgToTXT()
    
    Call GetMailInfo("C:\Users\Desktop\New Folder\")  'your folder with .msg files to be converted
    
    End Sub
    
    
    
    Sub GetMailInfo(Path As String)
    
        Dim MyOutlook As Outlook.Application
        Dim msg As Outlook.MailItem
        Dim x As Namespace
        Dim strContent As String
        
        Set MyOutlook = New Outlook.Application
        Set x = MyOutlook.GetNamespace("MAPI")
    
        FileList = GetFileList(Path + "*.msg")
        
        Row = 1
    
        While Row <= UBound(FileList) 'loops until processes all file in folder
            
            strContent = ""
            
            Set msg = x.OpenSharedItem(Path + FileList(Row))
            'creates .txt file in the folder where current excel is
            Open ThisWorkbook.Path & "\" & msg.Sender + Format(msg.SentOn, "DD.MM.YYYY") & ".txt" For Output As #1
            
            'collect some basic info from Outlook message in one string
            strContent = msg.Subject
            strContent = strContent + " " + msg.Sender
            strContent = strContent + " " + msg.CC
            strContent = strContent + " " + msg.To
            strContent = strContent + " " + Format(msg.SentOn, "DD.MM.YYYY, hh:mm")
            strContent = strContent + " " + msg.Body
                    
            Print #1, strContent 'write string o file
            
            Close #1 'close file
            
            Row = Row + 1
            
        Wend
    
    End Sub
    
    Function GetFileList(FileSpec As String) As Variant
    '   Taken from http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/
    '   Returns an array of filenames that match FileSpec
    '   If no matching files are found, it returns False
    
        Dim FileArray() As Variant
        Dim FileCount As Integer
        Dim FileName As String
    
        On Error GoTo NoFilesFound
    
        FileCount = 0
        FileName = Dir(FileSpec)
        If FileName = "" Then GoTo NoFilesFound
    
    '   Loop until no more matching files are found
        Do While FileName <> ""
            FileCount = FileCount + 1
            ReDim Preserve FileArray(1 To FileCount)
            FileArray(FileCount) = FileName
            FileName = Dir()
        Loop
        GetFileList = FileArray
        Exit Function
    
    '   Error handler
    NoFilesFound:
            GetFileList = False
    End Function
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. converting 32-bit files to 64-bit files.
    By bklangston in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 06-01-2016, 04:23 PM
  2. Converting files to XML
    By cost9170 in forum Excel General
    Replies: 1
    Last Post: 03-03-2016, 04:12 PM
  3. converting excel files into csv txt files
    By robfeels in forum Excel General
    Replies: 0
    Last Post: 08-08-2013, 08:01 AM
  4. Replies: 1
    Last Post: 02-29-2012, 01:15 PM
  5. converting files
    By bustanutti21 in forum Excel General
    Replies: 0
    Last Post: 05-19-2006, 01:37 PM
  6. [SOLVED] Converting Adobe files into Excel files
    By sweetsue516 in forum Excel General
    Replies: 2
    Last Post: 03-13-2006, 03:10 PM
  7. Converting CSV files into Excel files
    By baski in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-13-2005, 08:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1