Outlook VBA - 错误424对象必需错误 - 但我无法解决原因

时间:2015-01-26 16:53:41

标签: vba outlook outlook-vba

我们每天通过电子邮件发送数百张发票 - 所有这些都是PDF格式,对于我部门的大多数成员来说,他们只是将它们标记为已读并将它们移动到文件夹。我的文件夹名为“发票”,是我收件箱的子文件夹。我写了下面的代码,它在行上抛出错误424:

Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

我要做的就是检查电子邮件是否未读并且有pdf附件,然后将其移至我的“发票”文件夹。代码如下:

Sub Lazy()
On Error GoTo Lazy_err
    ' Declare the variables
    Dim ns As NameSpace
    Dim myInbox As Outlook.Folder
    Dim myDestFolder As Outlook.Folder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim i As Integer

    ' Set variables
    Set ns = GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myDestFolder = myInbox.Folders("invoices")
    i = 0

    ' If statement to check if there's any unread emails in the box
    If Inbox.UnReadItemCount = 0 Then
        MsgBox "There are no unread messages in your Inbox.", vbInformation, _
                "Nothing Found"
        Exit Sub
    End If

    For Each Item In Inbox.Items
        If Item.UnRead = True Then
            For Each Atmt In Item.Attachments
                If Right(Atmt.FileName, 3) = "pdf" Then
                    myItem.Move myDestFolder
                    Item.UnRead = False
                    i = i + 1
                End If
            Next Atmt
            ' close off If statements, then move to next item and start again
        End If
    Next Item

    ' Display a summary message!
    If i > 0 Then
        MsgBox "I found " & i & " emails." _
                & vbCrLf & "I have moved them into the correct folder." _
                & vbCrLf & vbCrLf & "Maybe double check to make sure nothing else has been moved?", vbInformation, "Finished!"
    Else
        MsgBox "There's nothing to find", vbInformation, _
            "Finished!"
    End If

    ' Housekeeping - reset everything for next time macro is run
Lazy_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    ' Exit the macro :)
    Exit Sub

    ' Error Handler - goes at very end of script, even after "exit sub"
Lazy_err:
    MsgBox "An unexpected error has occurred." _
            & vbCrLf & "Please note and report the following information." _
            & vbCrLf & "Macro Name: SaveAttachments" _
            & vbCrLf & "Error Number: " & Err.Number _
            & vbCrLf & "Error Description: " & Err.Description _
            , vbCritical, "Error!"
    Resume Lazy_exit
End Sub

2 个答案:

答案 0 :(得分:1)

首先,您需要更正Paul建议的名称空间变量名称:

 ' Set variables
Set ns = GetNamespace("MAPI")
Set myInbox = ns.GetDefaultFolder(olFolderInbox)

然后我注意到以下几行代码:

 For Each Item In Inbox.Items
    If Item.UnRead = True Then

不要迭代文件夹中的所有项目。这将花费大量时间并且可能导致与不及时释放对象相关的问题。请改用Items类的Find / FindNext或Restrict方法。您可以在以下文章中阅读有关这些方法的更多信息:

答案 1 :(得分:0)

您已创建/初始化了命名空间对象变量ns,但未创建myNameSpace。确保修改代码以引用适当的对象。

Sub Lazy()
On Error GoTo Lazy_err
    ' Declare the variables
    Dim ns As NameSpace
    Dim myInbox As Outlook.Folder
    Dim myDestFolder As Outlook.Folder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim i As Integer

    ' Set variables
    Set ns = GetNamespace("MAPI")
    Set myInbox = ns.GetDefaultFolder(olFolderInbox)
    'Code continues...