Reputation: 491
The code below is sourced from another SO post: Excel VBA Code to retrieve e-mails from outlook.
THe purpose is to find information from Outlook e-mails and put them into Excel.
Sub test2()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim eFolder As Outlook.Folder
Dim i As Long
Dim x As Date
Dim wb As Workbook
Dim ws As Worksheet
Dim iCounter As Long
Dim lrow As Long
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
wb.Activate
ws.Select
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
x = Date
For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(eFolder.Name)
For i = olFolder.Items.Count To 1 Step -1
If TypeOf olFolder.Items(i) Is MailItem Then
Set olMail = olFolder.Items(i)
For iCounter = 2 To lrow
If InStr(olMail.SenderEmailAddress, ws.Cells(iCounter, 5).Value) > 0 Then
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & lrow).Offset(1, 0).Value = olMail.Subject
.Range("A" & lrow).Offset(1, 1).Value = olMail.ReceivedTime
.Range("A" & lrow).Offset(1, 2).Value = olMail.SenderEmailAddress
End With
End If
Next iCounter
End If
Next i
Set olFolder = Nothing
Next eFolder
End Sub
WHen i debug and hover over the last few lines, it seems the code is extracting information from Outlook properly. However, the extracted data(subject of e-mail, etc) aren't populated in my worksheet. From what I can gather I've set the worksheet variable correctly, don't really know what's going on.
Thanks for all the help
Update:
Worksheet is populating now. I am trying to get the code to go through a column of e-mail addresses, and extract "time received" from the emails if the addresses match with those in my folders.
Upvotes: 1
Views: 428
Reputation: 3153
Made some changes. See if this works.
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim eFolder As Outlook.folder
Dim i As Long
Dim x As Date
Dim wb As Workbook
Dim ws As Worksheet
Dim iCounter As Long
Dim lrow As Long
Set wb = ActiveWorkbook
Set ws = wb.WorkSheets("Sheet1")
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
x = Date
'i think you want column E here, not L?
lastRow = ThisWorkbook.WorkSheets("Sheet1").Cells(Rows.Count, "L").End(xlUp).Row
For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(eFolder.name)
For i = olFolder.Items.Count To 1 Step -1
For iCounter = 2 To lastRow
If TypeOf olFolder.Items(i) Is MailItem Then
Set olMail = olFolder.Items(i)
If InStr(olMail.SenderEmailAddress, ws.Cells(iCounter, 5).Value) > 0 Then 'qualify the cell
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & lrow + 1).Value = olMail.SUBJECT
.Range("B" & lrow + 1).Value = olMail.ReceivedTime
.Range("C" & lrow + 1).Value = olMail.SenderEmailAddress
End With
End If
Next iCounter
End If
Next i
Set olFolder = Nothing
Upvotes: 2
Reputation: 35
Are the emails you're looking for in your inbox or a subfolder? The code is ONLY looking in each FOLDER in the inbox, it's not looking in the actual inbox.
Try these changes:
Dim i As Long, j As Long 'Add "j as long"
'For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
For j = 0 To olNs.GetDefaultFolder(olFolderInbox).Folders.Count ' loop through the folders, starting at 0 (which we'll call the inbox)
If j = 0 Then
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
Else
Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(j)
End If
...rest of loop
Next ' Remove 'efolder' from here
Upvotes: 0