Hello, All.
I would like to know what I am doing wrong for the case formular. I am trying to move emails to folders according to the sender's email addresses. I am have a problem where case function is stated...
Sub Billing()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim currentMailItem As MailItem
Dim senderemailaddress As MailItem
Dim atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim Subfolder As MAPIFolder
Dim VarResponse As VbMsgBoxResult
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
ii = i - 1
Set Subfolder = Inbox.Folders("Billing")
Set Item = Subfolder.Items
If Subfolder.UnReadItemCount = 0 Then
MsgBox "There is No New Fax Materials in Billing Folder", vbInformation, " No Invoice "
Exit Sub
End If
If Subfolder.UnReadItemCount > 0 Then
For Each Item In Subfolder.Items
For Each atmt In Item.Attachments
' THE PROBLEM IS THAT Case Item.senderemailaddress DOES NOT WORK.
Select Case Item.senderemailaddress
Case Item.senderemailaddress = "aaa@aaa.com"
FileName = "T:\Carriers\Carrier Invoice\aaa\" '& "aaa " & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
Case Item.senderemailaddress = "bbb@bbb.com"
FileName = "T:\Carriers\Carrier Invoice\bbb\" '& "bbb " & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
Case Else
FileName = "T:\Carriers\Carrier Invoice\" & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
End Select
atmt.SaveAsFile FileName
i = i + 1
Next atmt
Item.UnRead = False
Next Item
End If
If i > 0 Then
VarResponse = MsgBox(i & " attached files found!." _
& vbCrLf & "The files have been saved in T:\Carriers\Carrier Invoice\" _
& vbCrLf & "Would you like to view the files now?" _
, vbQuestion + vbYesNo, "Finished!")
If VarResponse = vbYes Then
Shell "Explorer.exe , T:\Carriers\Carrier Invoice\", vbNormalFocus
End If
Else
MsgBox "No new attachments found.", vbInformation, _
"Finished!"
End If
MoveEmailAttachments_exit:
Set ns = Nothing
Set atmt = Nothing
Set Inbox = Nothing
Set Subfolder = Nothing
Set Item = Nothing
Set currentMailItem = Nothing
Exit Sub
MoveEmailAttachments_Err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MoveEmailAttachments_exit:
End Sub
Please let me know what I am doing wrong. Thank you all for your time!!
I would like to know what I am doing wrong for the case formular. I am trying to move emails to folders according to the sender's email addresses. I am have a problem where case function is stated...
Sub Billing()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim currentMailItem As MailItem
Dim senderemailaddress As MailItem
Dim atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim Subfolder As MAPIFolder
Dim VarResponse As VbMsgBoxResult
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
ii = i - 1
Set Subfolder = Inbox.Folders("Billing")
Set Item = Subfolder.Items
If Subfolder.UnReadItemCount = 0 Then
MsgBox "There is No New Fax Materials in Billing Folder", vbInformation, " No Invoice "
Exit Sub
End If
If Subfolder.UnReadItemCount > 0 Then
For Each Item In Subfolder.Items
For Each atmt In Item.Attachments
' THE PROBLEM IS THAT Case Item.senderemailaddress DOES NOT WORK.
Select Case Item.senderemailaddress
Case Item.senderemailaddress = "aaa@aaa.com"
FileName = "T:\Carriers\Carrier Invoice\aaa\" '& "aaa " & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
Case Item.senderemailaddress = "bbb@bbb.com"
FileName = "T:\Carriers\Carrier Invoice\bbb\" '& "bbb " & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
Case Else
FileName = "T:\Carriers\Carrier Invoice\" & Format(Item.ReceivedTime, "yyyymmdd_hhnn_") & atmt.FileName
End Select
atmt.SaveAsFile FileName
i = i + 1
Next atmt
Item.UnRead = False
Next Item
End If
If i > 0 Then
VarResponse = MsgBox(i & " attached files found!." _
& vbCrLf & "The files have been saved in T:\Carriers\Carrier Invoice\" _
& vbCrLf & "Would you like to view the files now?" _
, vbQuestion + vbYesNo, "Finished!")
If VarResponse = vbYes Then
Shell "Explorer.exe , T:\Carriers\Carrier Invoice\", vbNormalFocus
End If
Else
MsgBox "No new attachments found.", vbInformation, _
"Finished!"
End If
MoveEmailAttachments_exit:
Set ns = Nothing
Set atmt = Nothing
Set Inbox = Nothing
Set Subfolder = Nothing
Set Item = Nothing
Set currentMailItem = Nothing
Exit Sub
MoveEmailAttachments_Err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MoveEmailAttachments_exit:
End Sub
Please let me know what I am doing wrong. Thank you all for your time!!