Importing email from outlook into excel using vba

kadenjp

New Member
Joined
Aug 20, 2015
Messages
6
I am working on a project where I can send a text to a list of phone numbers (through outlook) and now, I want to import the replies into a new worksheet automatically. I found and edited some code that seems to work for importing emails from a specific folder, but I cannot figure out how to get it to import emails from the inbox. Any help would be greatly appreciated. Here is the code I have:

Sub ImportEmails()
Dim olA As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olF As Outlook.MAPIFolder
Dim olM As Outlook.MailItem
Dim lrow As Long

Set olA = New Outlook.Application
Set olNS = olA.GetNamespace("MAPI")

'Select the folder number here where the first one is 0
Set olF = olNS.Folders.Item(0)
lrow = 1
For Each olM In olF.Items
With Sheets(Sheet3)
.Cells(lrow, 1) = olM.SenderName
.Cells(lrow, 2) = olM.Subject
.Cells(lrow, 3) = olM.Body
lrow = lrow + 1
End With
Next

Set olM = Nothing
Set olF = Nothing
Set olNS = Nothing
Set olA = Nothing
End Sub
 
Try replacing...

Code:
Set olF = olNS.Folders.Item(0)

with

Code:
Set olF = olNS.GetDefaultFolder(olFolderInbox)

Hope this helps!
 
Last edited:
Upvote 0
Yep, worked great! Thanks!

I realized that I had another small error calling the sheet. It worked now though.

Thanks again for the help.
 
Upvote 0

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