Importing Emails into Outlook when Parent Folder is Not the Inbox

scienceguy

New Member
Joined
Dec 28, 2018
Messages
2
Hello,

I am using the following code to import Outlook 365 emails into Excel. However, my parent folder is NOT the Inbox. Right-clicking on the account name, I created a folder called "PROJECTS". This would be at the same hierarchy (I believe) as the Inbox (much like in principle the Deleted Items folder is). How do I revise the following line to go to the PROJECTS folder:

Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)

Many thanks!
Roy



Sub GetFromOutlook()
'Adapted from https://www.howtoexcel.org/vba/how-to-import-your-outlook-emails-into-excel-with-vba/


Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer


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_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body

i = i + 1
End If
Next OutlookMail


Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing


End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi,
Try this:
Code:
  Set Folder = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk).Parent.Folders("PROJECTS")
  Debug.Print Folder.Name, Folder.Items.Count
Regards
 
Upvote 0
Glad it helped and thanks for the feedback! :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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