Hi guys,
Im trying to get the list of emails (subject, sender email and date) from Outlook.
I have a lot of mailboxes and thats que question. Because Im using a code that get the list, but I am not getting from the right mailbox.
Could you guys help me to do this?
The code arent working as expected, this is not thaaat code, but...
- It is extracting the list, ok, but
- The code gets the list from primary mailbox and I would like to choose the right mailbox
- The code does not delete the last search if the date defined find less emails rows
- The code format data as table and the way that I autofit columns isnt good I guess
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim tblExists As Boolean
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("From_date").Value Then
Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Subject").Offset(i, 0).VerticalAlignment = xlTop
Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_Date").Offset(i, 0).VerticalAlignment = xlTop
Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderEmailAddress
Range("email_Sender").Offset(i, 0).VerticalAlignment = xlTop
Range("A3").Select
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
'Check the same already exists
tblExists = False
For Each o In Sheets("Import").ListObjects
If o.Name = "OutlookEmail" Then tblExists = True
Next o
'If exists, delete the table first
If (tblExists) Then
Sheets("Import").ListObjects("OutlookEmail").Unlist
End If
Columns("A:C").EntireColumn.AutoFit
End Sub
Im trying to get the list of emails (subject, sender email and date) from Outlook.
I have a lot of mailboxes and thats que question. Because Im using a code that get the list, but I am not getting from the right mailbox.
Could you guys help me to do this?
The code arent working as expected, this is not thaaat code, but...
- It is extracting the list, ok, but
- The code gets the list from primary mailbox and I would like to choose the right mailbox
- The code does not delete the last search if the date defined find less emails rows
- The code format data as table and the way that I autofit columns isnt good I guess
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim tblExists As Boolean
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("From_date").Value Then
Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Subject").Offset(i, 0).VerticalAlignment = xlTop
Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_Date").Offset(i, 0).VerticalAlignment = xlTop
Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderEmailAddress
Range("email_Sender").Offset(i, 0).VerticalAlignment = xlTop
Range("A3").Select
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
'Check the same already exists
tblExists = False
For Each o In Sheets("Import").ListObjects
If o.Name = "OutlookEmail" Then tblExists = True
Next o
'If exists, delete the table first
If (tblExists) Then
Sheets("Import").ListObjects("OutlookEmail").Unlist
End If
Columns("A:C").EntireColumn.AutoFit
End Sub