Outlook-VBA to extract data Outlook mails to open Excel file

Hermac

New Member
Joined
Sep 5, 2016
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi,
With a for/next loop I want to run through a selection of Outlook-mails and extract the same 5 items from each one, to be dropped horizontally in an open Excelfile "MailStats.xlsm", Worksheet("Stats1")

Something like in the code below but with a destination to some horizontal range in Woksheets("Stats1") instead of msgbox.
I know how to pick the next open row with End(xlUp).Offset(1, 0). And I know how to make the VBA references between Outlook en Excel with the 16.0 Object Libraries
What I'm not familiar with is the most appropriate and efficient instruction code within Outlook-VBA to pick stuff up in Outlook and drop in an Excel Cell.
I mean especially the traffic between both applications.

Public Sub SaveMailsInFiles()
Dim oMail As Outlook.MailItem, objItem As Object
Dim sPath As String, dtDate As Date, sSubj As String
Dim sSendr As String, sRecip As String, enviro As String, strFolderpath As String, sNam As String
enviro = CStr(Environ("USERPROFILE"))
strFolderpath = BrowseForFolder(enviro & "\documents\MAILTRANSIT")
For Each objItem In ActiveExplorer.Selection
Set oMail = objItem
sNam = oMail.Subject
sSendr = oMail.SenderName: sRecip = oMail.To
ReplacementsInNam sNam, "-"
ReplacementsInSendr sSendr, "HVN": ReplacementsInRecip sRecip, "HVN"
dtDate = oMail.ReceivedTime
MsgBox strFolderpath
MsgBox oMail.Attachments.Count
MsgBox oMail.MessageClass
MsgBox oMail.SenderName
MsgBox sRecip[/I]
Next
??..
Thank you very much for any hint to get me on track.
Herman Van Noten
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Add this code to reference the next row in the "Stats1" sheet of the open "MailStats.xlsm" workbook:

VBA Code:
    Dim ExcelApp As Excel.Application
    Dim ExcelWb As Excel.Workbook
    Dim destCell As Excel.Range
    Dim r As Long
        
    'Attempt to attach to running Excel instance

    Set ExcelApp = Nothing
    On Error Resume Next
    Set ExcelApp = GetObject(, "Excel.Application")
    On Error GoTo 0

    If ExcelApp Is Nothing Then
        'Excel not running, so open new instance
        Set ExcelApp = New Excel.Application 'CreateObject("Excel.Application")
    End If
    
    Set ExcelWb = Nothing
    On Error Resume Next
    Set ExcelWb = ExcelApp.Workbooks("MailStats.xlsm")
    On Error GoTo 0
    
    If ExcelWb Is Nothing Then
        MsgBox "MailStats.xlsm isn't open", vbCritical
        Exit Sub
    End If
    
    With ExcelWb.Worksheets("Stats1")
        Set destCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    End With
and this loop to put the email details in the sheet rows:
VBA Code:
    r = 0
    For Each objItem In ActiveExplorer.Selection
        Set oMail = objItem
        With oMail
            destCell.Offset(r).Resize(, 6).Value = Array(.Subject, .SenderName, .To, .ReceivedTime, .Attachments.Count, .MessageClass)
        End With
        r = r + 1
    Next
 
Upvote 1
Solution
Wow, this is beautiful and does precisely what I wanted, John_W ! Thank you very much for making my day.
Herman
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top