We are trying to pare down the size of our Outlook PSTs
, by pulling out no longer relevant emails into various project folders when a project is done. So after realizing the copies you can just pull out of Outlook are virtually unsortable, and do not carry over any of their meta data, I went looking for other solutions. And found a few partial VBA scripts to do it, which i have cobbled together and changed here to try and get what i want.
The routine reads a selection from Outlook, and saves the emails to a provided location with the timestamps and either sender or receiver as needed. Sorted into sub folders. That part seems to work pretty well. But in my test, which I ran on a outlook folder of 238 emails my test-log had 233 entries, but only 231 files were output. Any ideas?
Could it be because the folder is too large? Such that i may need to do it in smaller segments. Or getting ahead of itself, such that i need to add a delay in there somewhere?
Option Explicit
Public Sub SaveMessageAsMsg()
Dim oMail As Outlook.MailItem
Dim objItem As Object
Dim sRootPath As String
Dim sPath As String
Dim dtDate As Date
Dim sDate As String
Dim sTime As String
Dim sName As String
Dim sFrom As String
Dim sTo As String
Dim sCC As String
Dim sBCC As String
Dim enviro As String
Dim sUser As String
Dim fso As Object
Dim log As Object
Dim count As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Set log = fso.CreateTextFile("C:\TestLog.txt", True)
count = 1
sUser = "UserName" 'During test this was the actual name
enviro = CStr(Environ("USERPROFILE"))
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Dim fd As Office.FileDialog
Set fd = xlApp.Application.FileDialog(msoFileDialogFolderPicker)
With xlApp.Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sRootPath = .SelectedItems(1)
End If
End With
Set fd = Nothing
xlApp.Quit
Set xlApp = Nothing
For Each objItem In ActiveExplorer.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem
sName = oMail.Subject
sName = RemoveSpecials(sName)
dtDate = oMail.ReceivedTime
sFrom = oMail.SenderName
sTo = oMail.To
sCC = oMail.CC
sBCC = oMail.BCC
sDate = Format(dtDate, "yyyy.mm.dd", vbUseSystemDayOfWeek, vbUseSystem)
sTime = Format(dtDate, "-hh.nn.ss", vbUseSystemDayOfWeek, vbUseSystem)
sPath = sRootPath
If InStr(sFrom, sUser) > 0 Then
sName = sDate + sTime + "_" + sUser + "_" + sName + ".msg"
sPath = sPath + "\To\"
ElseIf InStr(sCC, sUser) > 0 Then
sName = sDate + sTime + "_" + sFrom + "_" + sName + ".msg"
sPath = sPath + "\CC\"
ElseIf InStr(sBCC, sUser) > 0 Then
sName = sDate + sTime + "_" + sFrom + "_" + sName + ".msg"
sPath = sPath + "\BCC\"
Else
sName = sDate + sTime + "_" + sFrom + "_" + sName + ".msg"
sPath = sPath + "\Received\"
End If
If Dir(sPath, vbDirectory) = "" Then
MkDir sPath
End If
log.WriteLine (CStr(count) + "/" + CStr(ActiveExplorer.Selection.count) + " - " + sPath + sName)
oMail.SaveAs sPath + sName, olMSG
count = count + 1
End If
Next
End Sub
Function RemoveSpecials(strInput As String) As String
Dim strChars As String
strChars = "!£$%^&*()_+{}@~:<>?,./;'#[]-=`¬¦" & Chr(34)
Dim intIndex As Integer
For intIndex = 1 To Len(strChars)
strInput = Replace(strInput, Mid(strChars, intIndex, 1), "")
Next
RemoveSpecials = strInput
End Function