Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2020, 07:45 PM
travb81 travb81 is offline outlook vba - setting reference to subfolder, sometimes no folder items in collection Windows 10 outlook vba - setting reference to subfolder, sometimes no folder items in collection Office 2016
Novice
outlook vba - setting reference to subfolder, sometimes no folder items in collection
 
Join Date: Aug 2017
Posts: 11
travb81 is on a distinguished road
Default outlook vba - setting reference to subfolder, sometimes no folder items in collection


I'm trying to move an outlook item (email) to a particular subfolder.
This code usually works the first time.
But on subsequent times it doesn't... then other times it will, random results.

The problem always seems to be getting the subfolders of the Inbox folder... sometimes I get a count of 0 folders in that collection, which is where this will throw up the error "The attempted operation failed. An object could not be found."
The commented line below works the same as the three above.... sometimes it works.. sometimes not. Any ideas on what is going wrong?!

Code:
Code:
    Dim NS As Outlook.NameSpace
    Set NS = Application.GetNamespace("MAPI")
    
    Dim oOfficeFolder As Outlook.MAPIFolder
    Dim oOfficeInboxFolder As Outlook.MAPIFolder
    Dim oOfficeInboxSupplierFolder As Outlook.MAPIFolder
    
    Set oOfficeFolder = NS.Folders.Item("Office")
    Set oOfficeInboxFolder = oOfficeFolder.Folders.Item("Inbox")
    Set oOfficeInboxSupplierFolder = oOfficeInboxFolder.Folders.Item("Suppliers")
    'Set oOfficeInboxSupplierFolder = NS.Folders("Office").Folders("Inbox").Folders("Suppliers")
    oMailItem.Move oOfficeInboxSupplierFolder
Reply With Quote
  #2  
Old 09-17-2020, 08:43 PM
gmayor's Avatar
gmayor gmayor is offline outlook vba - setting reference to subfolder, sometimes no folder items in collection Windows 10 outlook vba - setting reference to subfolder, sometimes no folder items in collection Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Assuming the account name is Office then


Code:
Sub MoveSelectedMsg()
Dim olStore As Store
Dim olFolder As Folder
Dim olItem As MailItem
    On Error Resume Next
    Select Case Outlook.Application.ActiveWindow.Class
        Case olInspector
            Set olItem = ActiveInspector.currentItem
        Case olExplorer
            Set olItem = Application.ActiveExplorer.Selection.Item(1)
    End Select
    For Each olStore In Session.Stores
        If olStore = "Office" Then
            Set olFolder = olStore.GetDefaultFolder(olFolderInbox).folders("Suppliers")
            olItem.Move olFolder
            Exit For
        End If
    Next olStore
    Set olStore = Nothing
    Set olFolder = Nothing
    Set olItem = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-17-2020, 11:12 PM
travb81 travb81 is offline outlook vba - setting reference to subfolder, sometimes no folder items in collection Windows 10 outlook vba - setting reference to subfolder, sometimes no folder items in collection Office 2016
Novice
outlook vba - setting reference to subfolder, sometimes no folder items in collection
 
Join Date: Aug 2017
Posts: 11
travb81 is on a distinguished road
Default

Neat reply, thanks!

Except i'm still getting the same behaviour.

Code:
    Set olFolder = olStore.GetDefaultFolder(olFolderInbox).folders("Suppliers")
I can tell when it's going to fail, as
Code:
olStore.GetDefaultFolder(olFolderInbox).folders.count
will return 0.



Quote:
Originally Posted by gmayor View Post
Assuming the account name is Office then


Code:
Sub MoveSelectedMsg()
Dim olStore As Store
Dim olFolder As Folder
Dim olItem As MailItem
    On Error Resume Next
    Select Case Outlook.Application.ActiveWindow.Class
        Case olInspector
            Set olItem = ActiveInspector.currentItem
        Case olExplorer
            Set olItem = Application.ActiveExplorer.Selection.Item(1)
    End Select
    For Each olStore In Session.Stores
        If olStore = "Office" Then
            Set olFolder = olStore.GetDefaultFolder(olFolderInbox).folders("Suppliers")
            olItem.Move olFolder
            Exit For
        End If
    Next olStore
    Set olStore = Nothing
    Set olFolder = Nothing
    Set olItem = Nothing
End Sub
Reply With Quote
  #4  
Old 09-23-2020, 11:22 PM
travb81 travb81 is offline outlook vba - setting reference to subfolder, sometimes no folder items in collection Windows 10 outlook vba - setting reference to subfolder, sometimes no folder items in collection Office 2016
Novice
outlook vba - setting reference to subfolder, sometimes no folder items in collection
 
Join Date: Aug 2017
Posts: 11
travb81 is on a distinguished road
Default

Any suggestion on what might be going wrong?
Reply With Quote
  #5  
Old 08-15-2023, 04:07 AM
wodonnell wodonnell is offline outlook vba - setting reference to subfolder, sometimes no folder items in collection Windows XP outlook vba - setting reference to subfolder, sometimes no folder items in collection Office 2003
Novice
 
Join Date: May 2011
Posts: 2
wodonnell is on a distinguished road
Default

Was a resolution to this ever found? We have hit the exact same issue.

For one specific user our code runs fine the first time but on subsequent runs folders.count returns 0 even though there are subfolders.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Acces a subfolder as main folder over imap in outlook 2010 Bandit Outlook 0 10-05-2013 06:23 AM
Add folder and subfolder to Favorite ying06 Outlook 0 03-30-2012 10:41 AM
Setting a default folder for items to be linked Thrilling Outlook 1 03-12-2012 12:09 PM
How does Outlook decide which Sent Items folder? rowan.bradley Outlook 1 02-12-2011 01:43 PM
Export Subfolder ~ Now ENTIRE Archive Folder is missing? HELP! TwinCams Outlook 0 01-31-2011 11:24 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft