Code to move mail with certain attachment name? Does Not work

Status
Not open for further replies.

Cody99

New Member
Outlook version
Outlook 365 64 bit
Email Account
Exchange Server
I'm stuck on some code I want to run in outlook. Firstly, I can't understand when it runs, seems to be when I start outlook, but I can't step through the code in the VBA editor either.

Then I get = nothing on "objTargetFolder" when it is run.

The reason is that I receive e-mails that differ only in the name of the attachment, and that does not solve common rules in Outlook. So one of the attachments is called, for example, CALLS DAILY, I'll look for that in incoming mail attachments in the inbox Then I'll move that mail in that case to the inbox/outlookdata/Calls daily

Then the same with a few different variations on attachment names for different folders. ("report" and "statistics" code I haven't changed yet.)

can anyone see why "objTargetFolder" = nothing when run. Can't find the right folder?

//////////////////////////////////////////////////////////////////////////////

Public WithEvents objMails As Outlook.Items

Private Sub Application_Startup()
Set objMails = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objMails_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim strAttachmentName As String
Dim objInboxFolder As Outlook.Folder
Dim objTargetFolder As Outlook.Folder

'Ensure the incoming item is an email
If TypeOf Item Is MailItem Then
Set objMail = Item
Set objAttachments = objMail.Attachments

'Check if the incoming email contains one or more attachments
If objAttachments.Count > 0 Then
For Each objAttachment In objAttachments
strAttachmentName = objAttachment.DisplayName
Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)
'Check the names of all the attachments
'Specify the target folders
If InStr(LCase(strAttachmentName), "CALLS DAILY") > 0 Then
Set objTargetFolder = objInboxFolder.Folders("Mitt.namn@min-epost.com").Folders("OutlookData").Folders("CALLS DAILY")
ElseIf InStr(LCase(strAttachmentName), "report") > 0 Then
Set objTargetFolder = objInboxFolder.Folders("Report")
ElseIf InStr(LCase(strAttachmentName), "statistics") > 0 Then
Set objTargetFolder = objInboxFolder.Folders("Statistics")
End If
 
I can see that the code sees the attachment in an incoming mail:
attachment.JPG

But in the next step i cant recognize the "move to" folder?
snipNothing.JPG


The folder is named exactly the same and is located -> inbox/OutlookData/callsdaily
 
So i got a bit further, i use parent.folder and it works sometimes, but not all the time?
 
Relly no one in the vast forum that can help me? :)
Its really strange that it works now and then, but not always.
Also when is the code active? only once after a restart of outlook?
No sometimes it runs when an emal arrives, but sometimes it dosent?
This is my code now:

Code:
Public WithEvents objMails As Outlook.Items

Private Sub Application_Startup()
 Set objMails = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objMails_ItemAdd(ByVal Item As Object)
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim strAttachmentName As String
    Dim objInboxFolder As Outlook.Folder
    Dim objTargetFolder As Outlook.Folder
 
    'MSGbox just to se wehn scrip runs.
    MsgBox "Script running"
    
 
    'Ensure the incoming item is an email
    If TypeOf Item Is MailItem Then
       Set objMail = Item
       Set objAttachments = objMail.Attachments
 
       'Check if the incoming email contains one or more attachments
       If objAttachments.Count > 0 Then
          For Each objAttachment In objAttachments
              strAttachmentName = objAttachment.DisplayName
              Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)
              'Check the names of all the attachments
              'Specify the target folders
              If InStr(LCase(strAttachmentName), "calls daily") > 0 Then
                 Set objTargetFolder = objInboxFolder.Parent.Folders("OutlookData").Folders("calls daily")
              ElseIf InStr(LCase(strAttachmentName), "calls mtd") > 0 Then
                 Set objTargetFolder = objInboxFolder.Parent.Folders("OutlookData").Folders("calls mtd")
              'ElseIf InStr(LCase(strAttachmentName), "statistics") > 0 Then
              '   Set objTargetFolder = objInboxFolder.Folders("Statistics")
              End If
         Next
         'Move the email to specific folder
          objMail.Move objTargetFolder
       End If
    End If
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
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
T What code to use to move the custom field values to message body when sending e-mail ? Outlook VBA and Custom Forms 8
M code to move selected Outlook contacts to another folder Using Outlook 3
M VBA code needed to move from Outlook 2010 subfolder to Symantec Vault subfolde Using Outlook 0
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
F Color code certain INBOX emails Using Outlook 2
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
H Preventing the 'email address fetch from Exchange' crashing email reading code Exchange Server Administration 0
Aussie Outlook 365 Rule runs manually but returns the error code "an unexpected error has occurred" when incoming mail arrives Using Outlook 1
S Need code to allow defined starting folder and selection from there to drill down Outlook VBA and Custom Forms 10
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
P Color Code or highlight folders in Outlook 2016 Using Outlook 2
N Please advise code received new mail Using Outlook 0
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
S Excel vba code to manage outlook web app Using Outlook 10
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
Witzker Pls help to change the code for inserting date in Ol contact body Outlook VBA and Custom Forms 5
I Outlook 2003 shows html code when To: field is empty Using Outlook 7
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
Alex Cotton "invalid or unqualified reference" on code that should work Outlook VBA and Custom Forms 5
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
D Any updates or fixes that would make this code stop working just moving emails to another folder Outlook VBA and Custom Forms 1
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
M error code 0x8DE00006 Using Outlook 1
R VBA Code to permanently delete selected email Outlook VBA and Custom Forms 10
dyny723 Outlook 2016: Code to link a contact to emails received from that contact Outlook VBA and Custom Forms 1
D Command Button code will not execute. Any suggestions Please. Outlook VBA and Custom Forms 2
V VB script code to save a specific email attachment from a given email Outlook VBA and Custom Forms 14
J Custom form code doesn't run Outlook VBA and Custom Forms 2
E Outlook Form - Voting Responses Not Auto Processing If Form Contains Any Code Outlook VBA and Custom Forms 0
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
S Problem running Command button code Outlook VBA and Custom Forms 2
C Color Code New Calendar Items Using Outlook 2
B Automation error running VB macro code Outlook VBA and Custom Forms 8
S my vbscript button1_click code works on appointment created but not on opening an existing apntmn Outlook VBA and Custom Forms 16
M regarding the Create Contacts From Messages code Using Outlook 8
J Outlook 2010 VBScript editor does not run code at all Outlook VBA and Custom Forms 0
B VBA Code to create appointment from email Outlook VBA and Custom Forms 1
D VBA Code to strip Subject Line when replying or forwarding Using Outlook 3

Similar threads

Back
Top