Export Email addresses from Outlook into Excel

itsonlyme4

New Member
Joined
Mar 9, 2006
Messages
25
Not sure if this is the correct Forum for this question.. but here goes...

I have an existing Macro in Outlook that basically scans an entire Mailbox for Messages within a given data Range, dumps the data (date, email address and email message body) into an excel file and then converts the excel to .htm and places the .htm file output on a Network drive.

I would like to create a whole new Macro which will do ALMOST the same thing - utilizing part of the code from the first macro - I want to read all emails and put the email addresses ONLY into a excel file - excluding duplicates of course.

My problem is, When in Outlook, I do ALT/F11 to open the VB Editor and it pulls the code in from the first Macro.

I guess what I don't understand is - How to I create a new Macro from scratch ????

I tried going into Outlook/Tools/Macros and typing a new name and then clicking create - but it does the same thing !! it shows me the code from the first Macro!?

DO I need to create a new module or a whole new project??? Any guidance or help would be much appreciated.

signed,
a VBA Newbie
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi VBAN,

You can use the same module or another one, that is more of a housekeeping issue than anything else. But am I to understand you want to run this from Outlook and not Excel? And the email addresses you want to grab, are they the "From" addresses?
 
Upvote 0
Yes, the email addresses I want to grab are the 'From' addresses. and I need the email addresses.. not the quick names

I would prefer to run it from Outlook mostly because I didn't think I could program it in Excel to open outlook and grab the data I need.. AND.. I already have an Outlook Macro that I can just modify the code on..
 
Upvote 0
The exisiting code I have allows to pick a subfolder - I would like something that can be run against the entire INBOX and subfolders as well
 
Upvote 0
This is actual code that I am trying to modify to do what I want it to do.. which is basically scan an Outlook INbox (and subfolders) and place all of the 'FROM' email addresses in excel. I can't seem to get it right!!??

Rich (BB code):
Dim strMessageBody As String
Dim strAttachment As String
Dim dtStartDate As Date
Dim dtEndDate As Date
Dim globalRowCount As Long
 
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
 
Option Explicit
 
Sub Export()
   
   Dim olApp As Outlook.Application
   Dim olSession As Outlook.NameSpace
   Dim olStartFolder As Outlook.MAPIFolder
   Dim olDestFolder As Outlook.MAPIFolder
   Dim strprompt As String
   Dim recipient As String
   Dim localRowCount As Integer
   
   
   Set xlApp = CreateObject("Excel.Application")
   
   'Initialize count of folders searched
   globalRowCount = 1
   
   ' Get a reference to the Outlook application and session.
   Set olApp = Application
   Set olSession = olApp.GetNamespace("MAPI")
 
   ' Allow the user to input the start date
   strprompt = "Enter the start date to search from:"
   dtStartDate = InputBox(strprompt, "Start Date", Now() - 7)
 
   ' Allow the user to input the end date
   strprompt = "Enter the end date to search to:"
   dtEndDate = InputBox(strprompt, "End Date", Now())
   
  UserForm1.Show
   
   
   If (IsNull(dtStartDate) <> 1) And (IsNull(dtEndDate) <> 1) Then
 
      ' Allow the user to pick the folder in which to start the search.
      MsgBox ("Pick the source folder (Feedback)")
      Set olStartFolder = olSession.PickFolder
      
      ' Check to make sure user didn't cancel PickFolder dialog.
      If Not (olStartFolder Is Nothing) Then
         ' Start the search process.
         ProcessFolder olStartFolder
         MsgBox CStr(globalRowCount) & " messages were found."
      End If
   
   xlApp.Quit
  
'   strprompt = "Enter the recipient of the .html attachment in xxx@xxx.xxx format: "
'   recipient = InputBox(strprompt, "Recipient's email", "oln_dba@olntv.com")
  
'   DTSMailer strMessageBody, strAttachment
'   DTSMailer commented out b/c no DTS package reference available on Geeta's machine.
   
'   MsgBox "Email sent to " & recipient
   MsgBox "Process is complete.  Check K:\feedback\htm\ for available files."
 
   End If
End Sub
 
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
 
Dim i As Long
Dim ValidEmails As Long
ValidEmails = 0
 
For i = CurrentFolder.Items.Count To 1 Step -1
   If ((CurrentFolder.Items(i).ReceivedTime >= dtStartDate) And (CurrentFolder.Items(i).ReceivedTime < dtEndDate)) Then
   ValidEmails = ValidEmails + 1
   End If
Next
 

If CurrentFolder.Items.Count >= 1 And ValidEmails >= 1 Then
   
   Dim localRowCount As Integer
   Dim xlName As String
      
   Set xlBook = xlApp.Workbooks.Add
   Set xlSheet = xlBook.Worksheets(1)
     
   localRowCount = 1
   xlName = CStr(Format(dtStartDate, "MMDDYYYY")) & "_" & CurrentFolder.Name & "_feedback"
 
   xlSheet.Cells(localRowCount, 1) = "SUBJECT"
   xlSheet.Cells(localRowCount, 2) = "SENDER"
   xlSheet.Cells(localRowCount, 3) = "RECEIVED DATE"
   xlSheet.Cells(localRowCount, 4) = "MESSAGE BODY"
 
   
   ' Late bind this object variable,
   ' since it could be various item types
   Dim olTempItem As Object
   Dim olNewFolder As Outlook.MAPIFolder
 

   ' Loop through the items in the current folder.
   ' Looping through backwards in case items are to be deleted,
   ' as this is the proper way to delete items in a collection.
       For i = CurrentFolder.Items.Count To 1 Step -1
    
          Set olTempItem = CurrentFolder.Items(i)
    
          ' Check to see if a match is found
          If ((olTempItem.ReceivedTime >= dtStartDate) And (olTempItem.ReceivedTime < dtEndDate)) Then
            localRowCount = localRowCount + 1
            globalRowCount = globalRowCount + 1
            xlSheet.Cells(localRowCount, 1) = olTempItem.Subject
            xlSheet.Cells(localRowCount, 2) = olTempItem.SenderEmailAddress
            xlSheet.Cells(localRowCount, 3) = CStr(Format(olTempItem.ReceivedTime, "MM/DD/YYYY"))
            xlSheet.Cells(localRowCount, 4) = Replace(Replace(Replace(olTempItem.Body, Chr(9), " "), Chr(10) & Chr(10), Chr(10)), Chr(13), "")
            
          End If
       
       Next
   
   readability_and_HTML_export
   xlBook.SaveAs ("c:\feedback\xls\" & xlName & ".xls")
   
   
   ActiveWorkbook.PublishObjects.Add( _
   SourceType:=xlSourceSheet, _
   FileName:="c:\feedback\htm\" & xlName & ".htm", _
   Sheet:="Sheet1", _
   Source:="", _
   HtmlType:=xlHtmlStatic).Publish
    
'   strAttachment = strAttachment & "\\stm-fs1\finapps\dynamics\feedback\" & xlName & ".htm; "
   
   xlBook.Save
   xlBook.Close
 
End If
 

   ' Loop through and search each subfolder of the current folder.
   For Each olNewFolder In CurrentFolder.Folders
      If olNewFolder.Name <> "Deleted Items" And olNewFolder.Name <> "Drafts" And olNewFolder.Name <> "Export" And olNewFolder.Name <> "Junk E - mail" And olNewFolder.Name <> "Outbox" And olNewFolder.Name <> "Sent Items" And olNewFolder.Name <> "Search Folders" And olNewFolder.Name <> "Calendar" And olNewFolder.Name <> "Contacts" And olNewFolder.Name <> "Notes" And olNewFolder.Name <> "Journal" And olNewFolder.Name <> "Shortcuts" And olNewFolder.Name <> "Tasks" And olNewFolder.Name <> "Folder Lists" And olNewFolder.Name <> "Inbox" Then
       
        ProcessFolder olNewFolder
      
      End If
   Next
End Sub
 

Private Sub readability_and_HTML_export()
'
' readability_and_HTML_export Macro

 
'
    Cells.Select
    Cells.EntireColumn.AutoFit
    Cells.EntireRow.AutoFit
    Columns("A:A").ColumnWidth = 32
'    Range("A1").Select
'    Range(Selection, Selection.End(xlDown)).Select
'    Range(Selection, Selection.End(xlToRight)).Select
    Cells.Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1:D1").Select
    With Selection.Interior
        .ColorIndex = 37
        .Pattern = xlSolid
    End With
    Selection.Font.Bold = True
    Columns("C:C").Select
    With Selection
        .HorizontalAlignment = xlLeft
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    If Columns("D:D").ColumnWidth < 80 Then
        Columns("D:D").ColumnWidth = 80
    End If
 
    If Columns("B:B").ColumnWidth > 40 Then
        Columns("B:B").ColumnWidth = 40
    End If
End Sub
 
 
 
'Private Sub DTSMailer(messagebody As String, attachmentstring As String)
Private Sub DTSMailer()
    Dim oPKG As New DTS.Package
    
    oPKG.LoadFromSQLServer "ol-dbsrvr-02", , , _
        DTSSQLStgFlag_UseTrustedConnection, , , , "Feedback_Mailer"
    oPKG.FailOnError = True
 
'    oPKG.GlobalVariables.Item("messagebody") = messagebody
'    oPKG.GlobalVariables.Item("attachmentstring") = attachmentstring
    
    oPKG.Execute
    oPKG.UnInitialize
    Set oPKG = Nothing
End Sub
 
Upvote 0
Well, I had something a little different than your routine. It's fairly basic, but it lists all the ReceivedByName's in column A of a worksheet and deletes any duplicates. Let me know if this is what you were looking for or not ...



<font face=Tahoma New><SPAN style="color:#00007F">Sub</SPAN> LoopThrough_SentReceived()
    
    <SPAN style="color:#00007F">Dim</SPAN> OL <SPAN style="color:#00007F">As</SPAN> Outlook.Application
    <SPAN style="color:#00007F">Dim</SPAN> NS <SPAN style="color:#00007F">As</SPAN> Outlook.Namespace, Inbox <SPAN style="color:#00007F">As</SPAN> Outlook.MAPIFolder, FL <SPAN style="color:#00007F">As</SPAN> Outlook.MAPIFolder
    <SPAN style="color:#00007F">Dim</SPAN> Email <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, IBitems <SPAN style="color:#00007F">As</SPAN> Outlook.Items
    <SPAN style="color:#00007F">Dim</SPAN> XL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, XLwb <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, XLws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> Cnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrHandle
    <SPAN style="color:#00007F">Set</SPAN> OL = ThisOutlookSession
    <SPAN style="color:#00007F">Set</SPAN> NS = GetNamespace("MAPI")
    <SPAN style="color:#00007F">Set</SPAN> Inbox = NS.GetDefaultFolder(olFolderInbox)
    <SPAN style="color:#00007F">Set</SPAN> IBitems = Inbox.Items
    <SPAN style="color:#00007F">Set</SPAN> XL = CreateObject("Excel.Application")
    XL.Visible = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> XLwb = XL.Workbooks.Add
    <SPAN style="color:#00007F">Set</SPAN> XLws = XLwb.Sheets(1)
    Cnt = 1
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> FL <SPAN style="color:#00007F">In</SPAN> Inbox.Folders
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Email <SPAN style="color:#00007F">In</SPAN> FL.Items
            Cnt = Cnt + 1
            XLws.Cells(Cnt, 1).Value = Email.ReceivedByName
        <SPAN style="color:#00007F">Next</SPAN> Email
    <SPAN style="color:#00007F">Next</SPAN> FL
    
ErrHandle:
    <SPAN style="color:#00007F">If</SPAN> Err <> 0 <SPAN style="color:#00007F">Then</SPAN>
        XL.Quit
    <SPAN style="color:#00007F">Else</SPAN>
        XLws.Cells(1, 1).Value = "From"
        XLws.Cells(1, 1).Font.Bold = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">For</SPAN> i = Cnt <SPAN style="color:#00007F">To</SPAN> 2 <SPAN style="color:#00007F">Step</SPAN> -1
            <SPAN style="color:#00007F">If</SPAN> XL.WorksheetFunction.CountIf(XLws.Range("A:A"), XLws.Cells(i, 1).Value) > 1 <SPAN style="color:#00007F">Then</SPAN> XLws.Cells(i, 1).EntireRow.Delete
        <SPAN style="color:#00007F">Next</SPAN> i
        MsgBox "Complete!"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Inbox = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> NS = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> XLws = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> XLwb = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> XL = <SPAN style="color:#00007F">Nothing</SPAN>
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thank you so much for the help!!! What I did was create a new Module in Outlook and ran the code you posted.
What it did was populate column A of a new worksheet with my name as
lastname, FirstName down to Row 209 with a ton of blank rows throughout. Looking at the code, I can't really figure out how to get it to just pull out smtp email addresses (not the names)
 
Upvote 0
Well, I thought I had it working. I changed some things after I posted, but now I can't get into my Outlook. Let me try to fix my app and try to retreive the data. If you haven't heard from me in a few days or so, feel free to pm me. Sorry about this.
 
Upvote 0

Forum statistics

Threads
1,224,903
Messages
6,181,653
Members
453,059
Latest member
jkevin

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