使用VBA按主题另存为Outlook中的MailItems

时间:2018-05-15 12:46:17

标签: vba for-loop outlook select-case

我在这里挣扎着。我正在尝试使用主题行中的第一个字符将“另存为邮件项目”保存到计算机上的不同文件夹中。

由于某种原因,该行: “Case oMail.Subject like”TEST1 *“

抛出错误:“对象变量或未设置块变量”

我不知道为什么!你能帮忙吗?

这是释义的脚本:

   Sub SavetoComputer() 
Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Dim oMail As Outlook.MailItem: Set oMail = Item
Dim SaveFolder As String
Dim TempSubject As String
Dim Serial As Integer, i As Integer

i = 0

For Each Item In olFolder.Items
    Serial = 0
    If TypeOf Item Is Outlook.MailItem Then
        Select Case True

        'Test case 1
        Case oMail.Subject Like "TEST1*"
        SaveFolder = "C:\Folder\Folder\Folder\"
        'Test case 2
        Case oMail.Subject Like "TEST2&"
        SaveFolder = "C:\Folder\Folder\Folder2\"
        Case Else
            i = i + 1
        End Select

On Error GoTo Continue
        oMail.SaveAs SaveFolder & oMail.Subject

Continue:
TempSubject = oMail.Subject & "Update: " & (Serial + 1)

    oMail.SaveAs SaveFolder & TempSubject
    End If
Next

Debug.Print i & " Message(s) were not sorted."

End Sub

我非常感谢任何帮助!

菲尔

1 个答案:

答案 0 :(得分:1)

未设置oMailItem。尝试在循环中设置oMailItem:

For Each Item In olFolder.Items
    Serial = 0
    If TypeOf Item Is Outlook.MailItem Then
       Dim oMail As Outlook.MailItem: Set oMail = Item