STEVENS3010
Board Regular
- Joined
- Feb 4, 2020
- Messages
- 89
- Office Version
- 365
- 2016
- Platform
- Windows
Hi all,
Is anybody able to help with the below code? I'm trying to import emails into an excel sheet from a shared mailbox but there seems to be a problem. I've tried the code from my own personal mailbox and it works so not sure what the issue is. The debug error message seems to point to the inbox name. I've used a dummy name in the code below. Can anybody point me in the right direction?
Is anybody able to help with the below code? I'm trying to import emails into an excel sheet from a shared mailbox but there seems to be a problem. I've tried the code from my own personal mailbox and it works so not sure what the issue is. The debug error message seems to point to the inbox name. I've used a dummy name in the code below. Can anybody point me in the right direction?
VBA Code:
Sub getEmails()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("dummyinobxname@workemail.co.uk")
Set olFldr = olFldr.Folders("Inbox")
ws.Cells.Clear
iRow = 2
Application.ScreenUpdating = False
For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
ws.Cells(iRow, "A") = .Subject
ws.Cells(iRow, "B") = .ReceivedTime
ws.Cells(iRow, "C") = .Categories
iRow = iRow + 1
End With
End If
Next olItem
With ws
hdr = Array("Subject", "ReceicedTime", "Categories")
.Range("A1").Resize(, UBound(hdr)) = hdr
.Columns.AutoFit
End With
Application.ScreenUpdating = False
End Sub