Show your folder tree where "CALLS DAILY" folder is visible.
I need help with VBA in Outlook, move attachment.
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 i run the code. Can't find the right folder?
And also, i cant understand when it runs, on startup of outlook, when a mail arrives, i cant see when, only that it sometimes run?!
//////////////////////////////////////////////////////////////////////////////
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("* Email address is removed for privacy *").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
3 answers
Sort by: Most helpful
-
-
Cody99 6 Reputation points
2022-10-12T16:13:20.043+00:00 Hi victor.
This is how it looks:
I also tried this now: Set objTargetFolder = objInboxFolder.Parent.Folders("OutlookData").Folders("callsdaily")IE, parent.folder first
I have changed the folder to have a space between the words and so on, but its always the same in the code as in the folder.
-
Victor Ivanidze 101 Reputation points
2022-10-12T16:29:42.79+00:00 Use
Set objTargetFolder = objInboxFolder.Parent.Folders("OutlookData").Folders("callsdaily")