How to Move Mail item after processing

Status
Not open for further replies.
Outlook version
Outlook 2007
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
 
Outlook version
Outlook 2007
Email Account
POP3
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: 292

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
The "Inbox" variable is not declared, and neither that one nor the "myInbox" variable is set to any folder.
 

Diane Poremsky

Senior Member
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")
 
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
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
L Outlook 2010- Be able to move default Mail folders (Outbox, Draft, etc) Outlook Wishlist 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
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
P Move emails between 2 mailboxes. Using Outlook 0
C Copy Move item won't work Outlook VBA and Custom Forms 2
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
Commodore Move turns into "copy" Using Outlook 3
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
Jennifer Murphy Ctrl+Tab sometimes will not move through text a word at a time Using Outlook 1
V Outlook 2016 will not move emails in search results Using Outlook 4
M move to iCloud not working in outlook calendar Using Outlook 12
A Create date folder and move messages daily Outlook VBA and Custom Forms 1
Commodore Folders always closed in move/copy items dialog box Using Outlook 3
C Move Outlook 2007 to new PC with Outlook 365 Using Outlook 3
C Can't move folder, the folder is full Using Outlook 0
Nadine Rule to move attachments with specific name Outlook VBA and Custom Forms 1
A Move email items based on a list of email addresses Outlook VBA and Custom Forms 40
T Move calendar invites to new calendar Using Outlook 5
O Rule to move (specific) messages from Sent folder to Specific folder Using Outlook 1
I Automating message move between folders Outlook VBA and Custom Forms 0
K Outlook Rules: Move a Copy Using Outlook 4
K VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
N Move emails of same conversation to same subfolder Using Outlook 6
D Move Emails between Folders in Separate Mailbox Outlook VBA and Custom Forms 12

Similar threads

Top