James MK
James MK

Reputation: 1

Outlook VBA - Error 424 Object Required error - but I can't work out why

We get hundreds of invoices emailed in per day - all are PDF format, and for most members of my dept, they're doing nothing more than marking them as read and moving them to a folder. My folder is called "invoices" and is a subfolder to my Inbox. I have written the following code, it throws an error 424 on the lines:

Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

All I'm trying to do is check if an email is unread and has a pdf attachment, then move it to my "invoices" folder. Code follows:

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

Upvotes: 0

Views: 3568

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

First of all, you need to correct the namespace variable name as Paul suggested:

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

Then I have noticed the following lines of code:

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

Don't iterate over all items in the folder. It will take a lot of time and may cause issues related to not releasing objects in time. Use the Find/FindNext or Restrict methods of the Items class instead. You can read more about these methods in the following articles:

Upvotes: 1

PaulFrancis
PaulFrancis

Reputation: 5809

You have created/initialized a Namespace Object variable ns, but not myNameSpace. Make sure you modify your code to reference appropriate objects.

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...

Upvotes: 0

Related Questions