vba 根据发件人姓名将电子邮件移动到文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17412035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving emails to folders based on SenderName
提问by Dryden Long
I have the following Visual Basic script that should move emails in my Inbox to specific folders but when I run it, nothing happens. I am very new to VBA so am a little confused as to why. Does anything stick out, or do you have any suggestions as how to find out why this is('nt) happening? Thanks!
我有以下 Visual Basic 脚本,可以将收件箱中的电子邮件移动到特定文件夹,但是当我运行它时,没有任何反应。我对 VBA 很陌生,所以我有点困惑为什么。有什么突出的,或者你有什么建议如何找出为什么会发生这种情况?谢谢!
Code:
代码:
Sub Move_Emails()
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(6)
Set myItems = myInbox.Items
Dim myItem As Outlook.MailItem
Dim MailItem As Object
Dim sn As String
For Each MailItem In myInbox.Items
sn = MailItem.SenderName
If sn = "John Doe" Then
Set myDestFolder = myInbox.Folders("Folder1")
ElseIf sn = "Jane Smith" Then
Set myDestFolder = myInbox.Folders("Folder2")
ElseIf sn = "Bob Jones" Then
Set myDestFolder = myInbox.Folders("Folder3")
End If
Set myItem = myItems.Find("[SenderName] = sn")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
Next
End Sub
回答by Kazimierz Jawor
You need to change the way you set your myItem variable
. In your code sn
is a variable and if you put it inside quotation marks it's not converted to real sender name. So, instead of this line:
您需要更改设置myItem variable
. 在您的代码中sn
是一个变量,如果您将它放在引号内,它不会转换为真正的发件人姓名。所以,而不是这一行:
Set myItem = myItems.Find("[SenderName] = sn")
use this line:
使用这一行:
Set myItem = myItems.Find("[SenderName]='" & sn & "'")
Editregarding possible problem according to comments below... When you check for the name in this way:
根据下面的评论编辑关于可能的问题......当您以这种方式检查名称时:
If sn = "John Doe" Then
you check for exact name of John Doe
including upper/lower cases. I suggest to change it in this way:
您检查John Doe
包括大写/小写的确切名称。我建议以这种方式改变它:
If Ucase(sn) = "JOHN DOE" Then
to avoid possible problems with names spelling. Do it for all checks in If statement
.
以避免可能出现的名称拼写问题。为所有签入执行此操作If statement
。
Edit 2nd... I have just realised that you use incorrect loop for moving elements. If you move one element to other folder as a result you change the order of your looping when using For each loop
. Therefore I suggest some more changes as described below in new complete code:
编辑第二个......我刚刚意识到您对移动元素使用了不正确的循环。如果你将一个元素移动到另一个文件夹,你会在使用For each loop
. 因此,我建议在新的完整代码中进行更多更改,如下所述:
Sub Move_Emails_improved()
Dim myNamespace, myInbox, myItems ', myDestFolder- NEW CHANGED MOVED TO SEPARATE LINE BELOW
Set myNamespace = Application.GetNamespace("MAPI")
Set myInbox = myNamespace.GetDefaultFolder(6)
Set myItems = myInbox.items
Dim myItem As Outlook.MailItem
Dim MailItem As Object
Dim sn As String
'NEW LINE BELOW
Dim myDestFolder As Folder
'here you need different kind of loop
Dim i as integer
For i = myInbox.items.Count To 1 Step -1 'loop goes from last to first element
sn = myInbox.items(i).SenderName
'first possible problem
If Ucase(sn) = "JOHN DOE" Then
Set myDestFolder = myInbox.folders("Folder1")
'alternatively you could check name in this way
ElseIf UCase(sn) Like "*JANE SMITH*" Then
Set myDestFolder = myInbox.folders("Folder2")
ElseIf sn = "Bob Jones" Then
Set myDestFolder = myInbox.folders("Folder3")
End If
Set myItem = myItems.Find("[SenderName]='" & sn & "'")
'here we need to check if folder is not set
'NEW- THIS LINE IMPROVED
While TypeName(myItem) <> "Nothing" And And Not myDestFolder Is Nothing
myItem.Move myDestFolder
Set myItem = myItems.FindNext
'NEW LINE BELOW
i = i - 1
Wend
'and set destination folder to nothing to eliminate all problems
Set myDestFolder = Nothing
Next
End Sub
Hope it will work now.
希望它现在会起作用。
回答by Ranjeet Srivastav
You can use also this:
你也可以使用这个:
If myitem.Sender Like "*" & sn & "*" Then
' your code