Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2016, 09:21 PM
gmayor's Avatar
gmayor gmayor is offline Saving group of individual e-mails Windows 10 Saving group of individual e-mails Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default


When reading an e-mail message in Outlook, you can indeed save it as an individual file. The default is MSG format, but you will need Outlook to open that format in order to view it. You could save as MHTML format which will open in Internet Explorer (which everyone using a PC should have) or in Word.

It is relatively straightforward to save a folder full of messages as msg format. Naming the files from the messages so that they are identifiable, unique and do not contain illegal filename characters is where the job gets a little more complicated.

Download, extract from the zip and import frmProgress from my web site http://www.gmayor.com/Forum/frmProgress.zip into the Outlook VBA editor (File > Import File) Then copy the following to a new module. Run ProcessFolder and follow the on-screen prompts.

Code:
Option Explicit

Sub ProcessFolder()
'An Outlook macro by Graham Mayor - www.gmayor.com
Dim olNS As Outlook.NameSpace
Dim olMailFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim olMailItem As Outlook.MailItem
Dim i As Long
Dim sPath As String
Dim ofrm As New frmProgress
Dim PortionDone As Double

    On Error GoTo err_Handler
    Set olNS = GetNamespace("MAPI")
    Set olMailFolder = olNS.PickFolder

    sPath = InputBox("Enter the path to save the messages." & vbCr & _
                     "The path will be created if it doesn't exist.", _
                     "Save Message", "C:\Path\")
    Do Until Right(sPath, 1) = Chr(92)
        sPath = sPath & Chr(92)
    Loop
    CreateFolders sPath

    Set olItems = olMailFolder.Items
    ofrm.Show vbModeless
    i = 0
    For Each olMailItem In olItems
        i = i + 1
        PortionDone = i / olItems.Count
        ofrm.Caption = "Processing " & i & " of " & olItems.Count
        ofrm.lblProgress.Width = ofrm.fmeProgress.Width * PortionDone
        SaveMessage olMailItem, sPath
        DoEvents
    Next olMailItem
    Unload ofrm
lbl_Exit:
    Set ofrm = Nothing
    Set olNS = Nothing
    Set olMailFolder = Nothing
    Set olItems = Nothing
    Set olMailItem = Nothing
    Exit Sub
err_Handler:
    MsgBox Err.Number & vbCr & Err.Description
    Err.Clear
    GoTo lbl_Exit
End Sub

Private Sub SaveMessage(olItem As MailItem, sPath As String)
'An Outlook macro by Graham Mayor - www.gmayor.com
Dim fname As String

    fname = Format(olItem.ReceivedTime, "yyyymmdd") & Chr(32) & _
            Format(olItem.ReceivedTime, "HH.MM") & Chr(32) & olItem.SenderName & " - " & olItem.subject
    fname = Replace(fname, Chr(58) & Chr(41), "")
    fname = Replace(fname, Chr(58) & Chr(40), "")
    fname = Replace(fname, Chr(34), "-")
    fname = Replace(fname, Chr(42), "-")
    fname = Replace(fname, Chr(47), "-")
    fname = Replace(fname, Chr(58), "-")
    fname = Replace(fname, Chr(60), "-")
    fname = Replace(fname, Chr(62), "-")
    fname = Replace(fname, Chr(63), "-")
    fname = Replace(fname, Chr(124), "-")
    SaveUnique olItem, sPath, fname
lbl_Exit:
    Exit Sub
End Sub

Private Function SaveUnique(oItem As Object, _
                            strPath As String, _
                            strFileName As String)
'An Outlook macro by Graham Mayor - www.gmayor.com
Dim lngF As Long
Dim lngName As Long
Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    lngF = 1
    lngName = Len(strFileName)
    Do While oFSO.FileExists(strPath & strFileName & ".msg") = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    oItem.SaveAs strPath & strFileName & ".msg"
lbl_Exit:
    Set oFSO = Nothing
    Exit Function
End Function

Private Function CreateFolders(strPath As String)
'An Office macro by Graham Mayor - www.gmayor.com
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    vPath = Split(strPath, "\")
    strPath = vPath(0) & "\"
    For lngPath = 1 To UBound(vPath)
        strPath = strPath & vPath(lngPath) & "\"
        If Not oFSO.FolderExists(strPath) Then MkDir strPath
    Next lngPath
lbl_Exit:
    Set oFSO = Nothing
    Exit Function
End Function
It seems most likely that you will have to digitally sign the VBA project in Outlook for the macro to work - see http://www.gmayor.com/create_and_emp...gital_cert.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #2  
Old 06-04-2016, 12:17 PM
gerryex gerryex is offline Saving group of individual e-mails Windows 10 Saving group of individual e-mails Office 2016
Novice
Saving group of individual e-mails
 
Join Date: Dec 2015
Location: Florida
Posts: 14
gerryex is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
When reading an e-mail message in Outlook, you can indeed save it as an individual file.
. . . .
Hi gmayor,

Thanks very much for the detailed info! I used to work on a database project in Access VBA so while I'm a little rusty I'm pretty sure I can get your code into Outlook. But I found VERY INTERESTING the complicated process to get the needed certificate validated in order for the VBA code to run. How on earth did you find this all out? I don't have time right now to give it all a try but I will soon and I'll let you know how it works out.

Thanks again,
Gerry
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving group of individual e-mails Saving Mails as PDF Michel777 Outlook 2 01-11-2015 07:54 AM
Saving sent mails to specific folder kammil121 Outlook 0 10-22-2014 02:26 AM
Saving a contact group received from someone else kcmihrguy Outlook 0 08-20-2014 06:20 AM
Saving senders email to contact group RicWCO Outlook 0 03-26-2012 10:03 PM
Saving group of individual e-mails group incoming e-mails into 2 search folders taher Outlook 1 11-07-2011 11:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft