Consulting

Results 1 to 4 of 4

Thread: Loop through non-default Outlook folder

  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.

  2. #2
    Sorry - reply added to wrong message
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    For reference, I resolved this as below.

    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder
     
    Set objNS = GetNamespace("MAPI")
       
    Set objFolder = objNS.Folders("Mailbox - MyName").Folders("Inbox").Folders("Test Sub Folder")
      
       
           For Each item In objFolder.Items
               
               'Do stuff
    
           Next
     
    End Sub

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    The original code may still be useful to you if you want to avoid typing in the multiple .Folders


    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 
        
        Debug.Print strFolderPath
        ' Drop \\ if passed into the function
        strFolderPath = Replace(strFolderPath, "\\", "")
        Debug.Print strFolderPath
        
        ' Fix possibly incorrectly facing slashes, when path is manually input
        strFolderPath = Replace(strFolderPath, "/", "\")    ' <--- Error was here
        Debug.Print 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
        
        ' Stepping through from LoopFolder with F8
        '  you would have seen objFolder is nothing
        Set GetFolder_Z = objFolder
         
        Set colFolders = Nothing
        Set objNS = Nothing
        Set objApp = Nothing
    End Function
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

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
  •