Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 



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 09:36 AM.


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