Consulting

Results 1 to 4 of 4

Thread: Loop through non-default Outlook folder

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    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.


    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
    Last edited by BrI; 04-26-2017 at 02:08 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •