使用VBA从发件人移动文件

时间:2013-11-27 16:37:03

标签: vba outlook-2010

我对VBA非常不熟悉。我没有规则的空间,我们没有root访问我们的Exchange服务器所以我需要使用脚本将电子邮件从22个不同的发件人移动到一个文件夹。我找到了下面的脚本,它似乎适用于其他的,但我在Sub线上得到一个语法错误,无法找到一种方法来使这项工作。我已经阅读了一些我可以在这里找到的帖子,我真的很难理解这里的语法。

 Sub MoveEmails()
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) = "FLPDMINFO" Then
    Set myDestFolder = myInbox.Folders("info")

'alternatively you could check name in this way
ElseIf UCase(sn) Like "GAPDMINFO*" Then
    Set myDestFolder = myInbox.Folders("info")
ElseIf sn = "DEPDM-INFO" Then
    Set myDestFolder = myInbox.Folders("ifnfo")
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

    i = i - 1

Wend
Set myDestFolder = Nothing
Next
End Sub

任何输入都将不胜感激。

1 个答案:

答案 0 :(得分:0)

你有两个'和'底部。改变这个:

While TypeName(myItem) <> "Nothing" And **And** Not myDestFolder Is Nothing

用这个:

While TypeName(myItem) <> "Nothing" And Not myDestFolder Is Nothing