Loop through non-default Outlook folder
Trying to loop through mail items in a non-default Outlook folder but getting "Object Required" error before loop as shown below.
I'm quite uncertain about the syntax and any help appreciated.
Code:
Sub LoopFolder()
Dim objOL As Outlook.Application
Set objOL = CreateObject("Outlook.Application")
Dim objMsg As Outlook.MailItem
Dim strFolderPath As String
strFolderPath = "\\Mailbox - BC\Inbox\TestFolder"
Set objFolder = GetFolder_Z(strFolderPath)
For Each objMsg In objFolder '<------------------------- Error 424: Object Reqired
'Do Stuff in loop
Next objMsg
End Sub
Function GetFolder_Z(strFolderPath As String) As MAPIFolder
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "\\", "")
strFolderPath = Replace(strFolderPath, "\", "/")
arrFolders() = Split(strFolderPath, "\")
Set objApp = Application
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetFolder_Z = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function