Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2016, 07:40 AM
paul h paul h is offline Exporting Messages to Excel using VBA Windows 7 32bit Exporting Messages to Excel using VBA Office 2007
Novice
Exporting Messages to Excel using VBA
 
Join Date: Jul 2015
Location: Richmond
Posts: 23
paul h is on a distinguished road
Default Exporting Messages to Excel using VBA

I found this bit of code online and have managed to run it with partial success. What I would like to do is narrow it down to scan a single folder and export the messages to an Excel file. Running it on the entire mailbox either takes to long or it errors out. I'm a novice Outlook programmer, though I have some experience in Access and Excel. I'm posting the entire module here, two subs and three functions. I'm hoping an Outlook guru can help me sort it out and simplify it.

Thanks,

Paul



Code:
Const MACRO_NAME = "OST2XLS"
Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    intMessages As Integer, _
    lngRow As Long
 
Sub ExportMessagesToExcel()
    Dim strFilename As String, olkSto As Outlook.Store
    strFilename = InputBox("C:\email\rejects.xls", MACRO_NAME)
    If strFilename <> "" Then
        intMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        For Each olkSto In Session.Stores
            Set excWks = excWkb.Worksheets.Add()
            excWks.Name = "Output"
            'Write Excel Column Headers
            With excWks
                .cells(1, 1) = "Folder"
                .cells(1, 2) = "Sender"
                .cells(1, 3) = "Received"
                .cells(1, 4) = "Sent To"
                .cells(1, 5) = "Subject"
                .cells(1, 6) = "Body"
            End With
            lngRow = 2
            ProcessFolder olkSto.GetRootFolder()
        Next
        excWkb.SaveAs strFilename
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intMessages & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub
 
 
Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.cells(lngRow, 1) = olkFld.Name
            excWks.cells(lngRow, 2) = GetSMTPAddress(olkMsg, intVersion)
            excWks.cells(lngRow, 3) = olkMsg.ReceivedTime
            excWks.cells(lngRow, 4) = olkMsg.ReceivedByName
            excWks.cells(lngRow, 5) = olkMsg.Subject
            excWks.cells(lngRow, 6) = olkMsg.Body
            lngRow = lngRow + 1
            intMessages = intMessages + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkSub = Nothing
End Sub
 
 
Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function
 
 
Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function
Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function
Reply With Quote
  #2  
Old 07-19-2016, 11:05 PM
gmayor's Avatar
gmayor gmayor is offline Exporting Messages to Excel using VBA Windows 10 Exporting Messages to Excel using VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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

If you only wish to process one folder, then you need to tell the macro which folder, and remove the sub folder references. The changes required to the first two macros are:
Code:
Option Explicit

Const MACRO_NAME = "OST2XLS"
Private excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    intMessages As Integer, _
    lngRow As Long

Sub ExportMessagesToExcel()
Dim strFilename As String
Dim olNS As NameSpace
Dim olFolder As Folder
Dim fso As Object

strFilename = InputBox("Enter path to save workbook", MACRO_NAME, "C:\email\rejects.xls")    'Folder must exist!
    If strFilename <> "" Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not (fso.FolderExists(Left(strFilename, InStrRev(strFilename, Chr(92))))) Then
            MsgBox "The folder " & Left(strFilename, InStrRev(strFilename, Chr(92))) & " does not exist!"
            GoTo lbl_Exit
        End If
        intMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        'excApp.Visible = True    'for testing
        Set olNS = GetNamespace("MAPI")
        Set olFolder = olNS.PickFolder
        Set excWks = excWkb.Worksheets.Add()
        excWks.Name = "Output"
        'Write Excel Column Headers
        With excWks
            .cells(1, 1) = "Folder"
            .cells(1, 2) = "Sender"
            .cells(1, 3) = "Received"
            .cells(1, 4) = "Sent To"
            .cells(1, 5) = "Subject"
            .cells(1, 6) = "Body"
            With .UsedRange
                .ColumnWidth = 22
                .HorizontalAlignment = 1
                .VerticalAlignment = -4160
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = -5002
                .MergeCells = False
            End With
        End With
        lngRow = 2
        ProcessFolder olFolder
        excWkb.SaveAs strFilename
    End If
    excApp.Quit
    MsgBox "Process complete.  A total of " & intMessages & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
lbl_Exit:
    Set fso = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set olFolder = Nothing
    Set olNS = Nothing
    Set excApp = Nothing
    Exit Sub
End Sub
 

Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object ', olkSub As Outlook.MAPIFolder
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.cells(lngRow, 1) = olkFld.Name
            excWks.cells(lngRow, 2) = GetSMTPAddress(olkMsg, intVersion)
            excWks.cells(lngRow, 3) = olkMsg.ReceivedTime
            excWks.cells(lngRow, 4) = olkMsg.ReceivedByName
            excWks.cells(lngRow, 5) = olkMsg.subject
            excWks.cells(lngRow, 6) = olkMsg.Body
            lngRow = lngRow + 1
            intMessages = intMessages + 1
        End If
        DoEvents 'Add this line
    Next
    Set olkMsg = Nothing
    'For Each olkSub In olkFld.folders
    '    ProcessFolder olkSub
    'Next
    'Set olkSub = Nothing
End Sub
__________________
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
  #3  
Old 07-21-2016, 07:01 AM
paul h paul h is offline Exporting Messages to Excel using VBA Windows 7 32bit Exporting Messages to Excel using VBA Office 2007
Novice
Exporting Messages to Excel using VBA
 
Join Date: Jul 2015
Location: Richmond
Posts: 23
paul h is on a distinguished road
Default

Just to clarify, this is the line where I will tell Outlook what folder to scan.

Code:
        Set olFolder = olNS.PickFolder
and will I pick the folder by clicking on it?

Also, are the three other functions no longer needed? I don't see them referenced in first two macros. I won't get to test this till this afternoon. I'm looking forward to putting it through its paces.
Reply With Quote
  #4  
Old 07-21-2016, 08:42 AM
paul h paul h is offline Exporting Messages to Excel using VBA Windows 7 32bit Exporting Messages to Excel using VBA Office 2007
Novice
Exporting Messages to Excel using VBA
 
Join Date: Jul 2015
Location: Richmond
Posts: 23
paul h is on a distinguished road
Default

I got my answers to the questions above. I easily picked out my folder. I ran the code as written and bumped into several problems which I will iterate. The code checks for olkMsg.Class = olMail. For my purposes this does not work. I am looking at Mail Delivery System Undeliverable notifications. For it to read any of these I had to comment out the If olkMsg.Class = olMail and End If. The next item was a
Quote:
Type Mismatch
on the GetSMTPAddress(olkMsg, intVersion) and then
Quote:
Outlook doesn't support this property or method
for olkMsg.RecerivedTime and olkMsg.ReceivedByName which I assume are not properties of notifications. I commented those three lines out and the code finished running. When it came time to save the files it took about 10 minutes, but it finally finished. The end product though was an empty file except for the headers. The objective is to collect all the email addresses found in the notification files and a few scattered messages. If I could get this code to just run for all the notifications it would be a big help.

Last edited by paul h; 07-21-2016 at 01:53 PM.
Reply With Quote
  #5  
Old 07-21-2016, 01:52 PM
paul h paul h is offline Exporting Messages to Excel using VBA Windows 7 32bit Exporting Messages to Excel using VBA Office 2007
Novice
Exporting Messages to Excel using VBA
 
Join Date: Jul 2015
Location: Richmond
Posts: 23
paul h is on a distinguished road
Default Chinese

Last chapter here. I've run both the original version on the messages and the modified version on the notifications. The messages came out fine. The notifications file came out literally full of Chinese characters. I'd post a picture, but I am unable to upload it.

Here's a sample. I'm not sure if this will display correctly.

Quote:
格浴㹬格慥㹤਍洼瑥⁡瑨灴攭畱癩∽潃瑮湥⵴祔数•潣瑮湥㵴琢硥⽴瑨汭※档牡敳㵴獵愭捳楩㸢⼼敨摡㰾潢祤ാ㰊 㹰戼㰾潦瑮挠汯牯∽〣〰㘰∶猠穩㵥㌢•慦散∽牁慩≬䐾汥癩牥⁹慨⁳慦汩摥琠桴獥⁥敲楣楰湥獴漠⁲牧畯獰㰺 是湯㹴⼼㹢⼼㹰਍昼湯⁴
Is there alternative way of harvesting this data?
Reply With Quote
  #6  
Old 07-21-2016, 10:20 PM
gmayor's Avatar
gmayor gmayor is offline Exporting Messages to Excel using VBA Windows 10 Exporting Messages to Excel using VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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

I don't have any ability to read Chinese, but Google translate suggests that this is spam. I am not sure why you would want to store this information? Might you not be better served by a decent spam filter. I have used MailWasher for years without problem.
__________________
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
  #7  
Old 07-22-2016, 06:37 AM
paul h paul h is offline Exporting Messages to Excel using VBA Windows 7 32bit Exporting Messages to Excel using VBA Office 2007
Novice
Exporting Messages to Excel using VBA
 
Join Date: Jul 2015
Location: Richmond
Posts: 23
paul h is on a distinguished road
Default

This is not spam. These are all
Quote:
Mail Delivery System
Undeliverable
messages.

Each one has an email address that was undeliverable.
I am trying to export these messages to Excel so I can harvest all these addresses.
The code does not capture this correctly, hence the "Chinese".
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting to Excel in MSP c991257 Project 7 05-15-2016 07:58 AM
Exporting Messages to Excel using VBA Exporting from excel into a specific format Bambi555 Excel 2 09-22-2014 08:08 AM
Exporting Messages to Excel using VBA Exporting Contacts to Excel misslinds Outlook 1 06-15-2014 08:08 AM
Exporting Messages to Excel using VBA * Exporting Access Data to Excel djreyrey Excel Programming 1 03-23-2012 10:03 PM
Exporting Messages to Excel using VBA Exporting to Excel lhicks Outlook 1 07-13-2011 02:02 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:08 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