How to Move Mail item after processing

Status
Not open for further replies.
Outlook version
Email Account
POP3
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.
Code:
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
        f2.Close
        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
Handler:
    MsgBox "An unexpected Error has occurred." _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    'Resume MsgErr_Exit
End Sub
 
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)
 

Attachments

  • Outlook_Script03.jpg
    Outlook_Script03.jpg
    16.1 KB · Views: 579
The "Inbox" variable is not declared, and neither that one nor the "myInbox" variable is set to any folder.
 
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")
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Move mail item after send Outlook VBA and Custom Forms 2
C Code to move mail with certain attachment name? Does Not work Outlook VBA and Custom Forms 3
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
P when i move inbox mails to another folder in outlook the mail disappears Using Outlook 1
O On click,I want to change subject line of selected mail and then reply to particular email and move Using Outlook 3
Mark White VBScript Move sent mail to non-default folder Outlook VBA and Custom Forms 5
Mark White VBScript Move sent mail to non-default folder Outlook VBA and Custom Forms 0
M Move new mail to folder based on sender address Outlook VBA and Custom Forms 2
D Help with code to move mail on receipt to another folder based on time received Outlook VBA and Custom Forms 2
S VBA Code to move mail items from search folder to inbox subfolder Outlook VBA and Custom Forms 4
L pin to Outlook move mail Using Outlook 2
T What code to use to move the custom field values to message body when sending e-mail ? Outlook VBA and Custom Forms 8
P move a specified email "From" tag items to a certain folder whenever there is "New Mail" in the inbo Outlook VBA and Custom Forms 5
Douglas Littlefield Macro to move E-mail Outlook VBA and Custom Forms 10
Douglas Littlefield Macro to move mail from inbox to Managed Folder Exchange Server Administration 1
L Macro Move E-mail attachments to a PC Folder Using Outlook 16
N rule to move sent mail to the deleted folder in outlook Using Outlook 4
C Outllok 2010 (14.0.7015.1000 (32bit) converts html mail to rich text when move Using Outlook 1
E Automatically move to other Folder from Inbox when mail comes - MS Outlook Using Outlook 1
J Macro to move mail from Inbox if older than 4 days Using Outlook 4
R How to remove or move the default Junk e-mail folder BCM (Business Contact Manager) 3
H Move Selected emails to Local Drive Outlook VBA and Custom Forms 0
A Search folder and move the email Outlook VBA and Custom Forms 0
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
humility36 Cannot move emails to archive - 440 error Outlook VBA and Custom Forms 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
C Trying to move messages between imap accounts/folders Using Outlook 5
M Move command Outlook VBA and Custom Forms 11
B Move emails from one account to another Outlook VBA and Custom Forms 2
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
N How to add or delete items to Move dropdown Menu Using Outlook 0
Commodore Unable to move message Using Outlook 3
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
C Move or copy from field to field Outlook VBA and Custom Forms 0
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
NVDon Create new Move To Folder list Outlook VBA and Custom Forms 0
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
J Dopey move - deleted profile Using Outlook 1
GregS Outlook 2016 Move Outlook to new computer? Using Outlook 4
Witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
D Move Email with Attachment to Folder Outlook VBA and Custom Forms 3
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
G Cannot Move Autocomplete File to New Computer Using Outlook 15
M Move to Folder Using Outlook 1

Similar threads

Back
Top