Move mails from Inbox to another folder

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have posted this question in another forum as well,


<p>

This is a requirement of an existing code. The below code works and saves the attachment and also marks the read mails as Processed.

But I need to move these Processed mails from the Inbox to the folder, Archive_Proc. I am trying to Set the folder, objDestfolder as the destination folder for these processed mails to be moved. But I am getting object not found error (bolded line).
How to fix the code?</p>

VBA Code:
    Dim olApp As Object
    Dim MYFOLDER As Object
    Dim OlItems As Object
    Dim olMail As Object
    Dim x As Integer
    Dim subject As String
    Dim strFile As String
    Dim strFolderpath As String
    Dim objDestfolder As Object
    Dim mychar As Object
    Dim sreplace As String



Set olApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
    Set olApp = CreateObject("Outlook.Application")
End If


strFolderpath = "C:\Users\Testing"
'On Error Resume Next


' Set the Attachment folder.
strFolderpath = strFolderpath & "\Attachments\"


    Set MYFOLDER = olApp.GetNamespace("MAPI").Folders("WeeklyProceedings Mailbox").Folders("Inbox")

    Set OlItems = MYFOLDER.Items
  [B]  Set objDestfolder = MYFOLDER.Folders("Archive_Proc")[/B]

    For Each olMail In OlItems
        If olMail.subject Like "*Proceeding ID*" Then
            strFile = olMail & ".XML"
            strFile = strFolderpath & strFile
            If olMail.Attachments.Count > 0 Then
                For x = 1 To olMail.Attachments.Count
                    olMail.Attachments.Item(x).SaveAsFile strFile
                Next x
                
                subject = olMail.subject
                sreplace = "_"
                subject = Replace(subject, " ", sreplace)
                olMail.Body = olMail.Body & vbCrLf & "The file was processed " & Now()
                olMail.subject = "Processed - " & subject
                olMail.Move objDestfolder
                olMail.Save
            End If
        End If
    Next

Set MYFOLDER = Nothing
Set OlMail = Nothing
Set OlItems = Nothing
Set olApp = Nothing
Srt objDestfolder = Nothing
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
So is your structure correct, as that folder should be in Inbox/Archive Folders ?
 
Upvote 0
So is your structure correct, as that folder should be in Inbox/Archive Folders ?
yes

1694622973255.png
 
Upvote 0
That is a terrible name for a folder,
Folders? :( Folders might be a reserved name to indicate folders?
Write some code to debug.print folders at each level. I *think* you would need "conversation history" in there?
The logic would be the same for file folders.
 
Upvote 0
Please advise the other forum that you have crossposted here.
If they discover themselves, you will likely be banned, and noone wants that. You need as many forums as you can access.
 
Upvote 0
Use this in Outlook, or amend to run from Access, to see your structures.

Code:
Sub EnumerateFoldersInStores()
 Dim colStores As Outlook.Stores
 Dim oStore As Outlook.Store
 Dim oRoot As Outlook.Folder
 
 On Error Resume Next
 Set colStores = Application.Session.Stores
 For Each oStore In colStores
 Set oRoot = oStore.GetRootFolder
 Debug.Print (oRoot.FolderPath)
 EnumerateFolders oRoot
 Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
 Dim folders As Outlook.folders
 Dim Folder As Outlook.Folder
 Dim foldercount As Integer
 
 On Error Resume Next
 Set folders = oFolder.folders
 foldercount = folders.Count
 'Check if there are any folders below oFolder
 If foldercount Then
 For Each Folder In folders
 Debug.Print (Folder.FolderPath)
 EnumerateFolders Folder
 Next
 End If
End Sub
 
Upvote 0
Solution
Try changing your bolded line to:
VBA Code:
Set objDestfolder = MYFOLDER..Folders("Folders").Folders("Archive_Proc")
 
Upvote 0
Use this in Outlook, or amend to run from Access, to see your structures.

Code:
Sub EnumerateFoldersInStores()
 Dim colStores As Outlook.Stores
 Dim oStore As Outlook.Store
 Dim oRoot As Outlook.Folder
 
 On Error Resume Next
 Set colStores = Application.Session.Stores
 For Each oStore In colStores
 Set oRoot = oStore.GetRootFolder
 Debug.Print (oRoot.FolderPath)
 EnumerateFolders oRoot
 Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
 Dim folders As Outlook.folders
 Dim Folder As Outlook.Folder
 Dim foldercount As Integer
 
 On Error Resume Next
 Set folders = oFolder.folders
 foldercount = folders.Count
 'Check if there are any folders below oFolder
 If foldercount Then
 For Each Folder In folders
 Debug.Print (Folder.FolderPath)
 EnumerateFolders Folder
 Next
 End If
End Sub
Thanks man! I was able to resolve my issue.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,150
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top