• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to Move Mail item after processing

Outlook version
Outlook 2007
Email Account
It has been suggested that all my tasks be included in a VBA script rather than some items in the Outlook rule and others in the script.
I am now trying to do just that.
Put simply I take the Outlook.item (containing words in the Subject) and save the body to a file available for processing on an MT4 (Forex trading) platform.
So I create a file. create a list of any files to be processed and then wish to move the item and that's where I fail.

This code performs the required task but fails with the move.item (I have tried numerous alternatives but I believe this shows what I want to achieve.
Public fs, f1, f2
Public filepath_part1 As String, filepath_part2 As String, filepath_full As String, myfname As String
Public Sub ChangeSubjectForward(item As Outlook.MailItem)
Dim str1 As String
Dim myline
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
    On Error GoTo Handler
    str1 = item.Body
    str2 = item.Subject
    'If InStr(str1, "Reversal") > 0 And InStr(str2, "Reversals-Signal") > 0 Then
    If InStr(str2, "Reversals-Signal") > 0 Then             '   search the subject
        getmyfile "ADS"                                     '   gets the appropriate file path and name attached to f2
        f2.writeline item.Body
        writeListofFiles                                    '   writes list of all files in the directory to a file to be accessed from MT4
        ' this is the code that has failed - I'd like to have a folder - say FolderA and sub folder FolderAsignals
        'Set myDestFolder = Inbox.Folders("FolderA")        ' changed for this example
        'item.Move myDestFolder
    End If
    Exit Sub
    MsgBox "An unexpected Error has occurred." _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    'Resume MsgErr_Exit
End Sub
Outlook version
Outlook 2007
Email Account
What`s the error message saying?
Thanks for the response.

The error occurs when trying to set the 'myDestFolder' and provides the error Object Could Not be Found (see attachment for full details)
I have used the above Folder names as example s but the real folder Exists within INBOX (and is not a sub folder)


Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Michael is referring to this - you dim these two:
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder

But you're setting the destination folder but not properly setting the inbox (or myinbox) object
Set myDestFolder = Inbox.Folders("FolderA")

The object and variable names you Dim need to match the names you use in the code.

see Working with VBA and non-default Outlook Folders for how to identify folders

this will work with a subfolder called folder a that is in the inbox:

Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set myInbox = Ns.GetDefaultFolder(olFolderinbox)
Set myDestFolder = myInbox.Folders("FolderA")

Similar threads