0

I try to write some VBA to save the attachment files from some email to a folder But I get the error

Run Time Error '424'

Object Required

This is the code I am trying to use

Sub test_extraer()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

If (Msg.SenderName = "sender@email.com") And _
       (Msg.Subject = "subject of the email") And _
   (Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String

    Const attPath As String = "C:\temp\"

   Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att
End If

End Sub

The error is triggered when the script enter to this if

If (Msg.SenderName = "sender@email.com") And _
       (Msg.Subject = "subject of the email") And _
       (Msg.Attachments.Count >= 1) Then

Any advice

Thanks in advance

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joseleg
  • 393
  • 9
  • 35
  • why is this tagged visual studio? – rory.ap Nov 06 '14 at 00:20
  • `Msg` is what here? You seem to be missing a step or two. – Tim Williams Nov 06 '14 at 01:01
  • 1
    The issues with this code are hurting my head. My first suggestion that will help pull out most of these errors is to use `Option Explicit` in this module. That will prevent you from trying to use undeclared variables which is what `msg` and `item` are right now – Matt Nov 06 '14 at 02:14

2 Answers2

0

Ok... where to start. You definitely have some basic issues you need to work out here. You have a couple of variables that are not declared. The first of which is the cause of your title. msg in context is most likely supposed to be an Outlook.MailItem. Just declaring that variable is not the sole source of your problems. Next you have item which much like msg in context should be an Outlook.MailItem. You are missing a loop that would navigate through all the items in the Inbox as well.

So you are just trying to navigate the Inbox looking for a particular item correct? Just adding the loop would create another issue. Some of the items in the inbox are not mail items. To address this we navigate every object in the inbox and examine every mailitem we come across. If that matches the criteria of sender,subject and number of items we proceed to .SaveAsFile to the destination directory.

Sub Test_ExtraER()

    Const strAttachmentPath As String = "C:\temp\"

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder
    Dim objItem As Object
    Dim strFileName As String

    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set objFolder = objNS.GetDefaultFolder(olFolderInbox)

    For Each objItem In objFolder.Items
        If TypeName(objItem) = "MailItem" Then
            If (objItem.Attachments.Count >= 1) And (objItem.Subject = "Some Subject") And (objItem.SenderName = "sender@email.com") Then
                With objItem.Attachments.Item(1)
                    strFileName = strAttachmentPath & .DisplayName
                    Debug.Print strFileName
                    .SaveAsFile strFileName
                End With
            End If
        End If
    Next
End Sub 

This is mostly preference but, as you can see, I made some other coding changes. I renamed some of the other variables to be a little more descriptive of the object it was. Also moved all the Dims and Const together for better readability.

One last thing. It would seem you are navigating you entire inbox looking for a small subset of mails. You could create a rule that would process these mails as they come into your mailbox. An example of this would be: Save Outlook attachment to disk

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
0
Sub test_extraer()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim MailItems As Outlook.MAPIFolder 'Add this one
Dim Msg As Outlook.MailItem 'Add this one
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set MailItems = objNS.GetDefaultFolder(olFolderInbox)

For Each Msg In MailItems.Items 'loop thru the inbox folder to match the exact sender name and subject
    If (Msg.SenderName = "Sender Name Here") And _
           (Msg.Subject = "Subject Here") And _
       (Msg.Attachments.Count >= 1) Then


        'Set folder to save in.
        Dim olDestFldr As Outlook.MAPIFolder
        Dim myAttachments As Outlook.Attachments
        Dim Att As String

        Const attPath As String = "C:\temp\"

       Set myAttachments = Msg.Attachments
        Att = myAttachments.Item(1).DisplayName
        myAttachments.Item(1).SaveAsFile attPath & Att
    End If
Next
End Sub
Ohw
  • 21
  • 2