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
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