Consulting

Results 1 to 13 of 13

Thread: Command button to download all attachments recieved today from a specific sender.

  1. #1
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location

    Command button to download all attachments recieved today from a specific sender.

    Good morning all,

    I am having a spot of bother with some code, I have pieced together most of it and keep getting the runtime error '438': Object doesn't support this property or method.

    Basically I need to be able to press a button which then downloads all attachments sent to my inbox today, from a specific sender. And I need to be able to generate the folder as well as this will be given to multiple employees in the company to use and they will not likely have the folder in question.

    Also, I think this code will only find the latest email, and not all emails delivered today. I am unsure of how to change this.

    Please see the code below:

    Private Sub CommandButton1_Click()
    Dim ol As Object 'Outlook.Application
    Dim Ns As Object 'Outlook.Namespace
    Dim i As Object
    Dim mi As Object 'Outlook.MailItem
    Dim inboxFol As Object 'Outlook.Folder
    Dim colItems As Object 'Outlook.Items
    Dim strFilter As String
    Dim resItems As Object
    Set ol = CreateObject(Class:="Outlook.Application")
    Set Ns = ol.GetNamespace("MAPI")
    Set inboxFol = Ns.GetDefaultFolder(6) 'olFolderInbox
    Set colItems = inboxFol.Items
    colItems.Sort "[SentOn]", False ' oldest to newest
    strFilter = "[SentOn]>'" & Format(Date, "DDDDD HH:NN") & "'"
    Debug.Print "strFilter .....: " & strFilter
    Set resItems = colItems.Restrict(strFilter)
    Debug.Print "resItems.Count: " & resItems.Count
    If resItems.Count Then
        For Each i In resItems
            If i.Class = 43 Then
                Set mi = i
                If mi.Attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
                    Debug.Print "Subject.....: " & mi.Subject
                    Debug.Print "SentOn .....: " & mi.SentOn
                    Dim trg As String, src As String
                    trg = "C:\user\attachments"
                    SpecialMkDir (trg)
                    mi.Attachments.SaveAsFile trg & mi.Attachments.Filename
                    VBA.FileCopy src, trg
                    Exit For ' Exit when the first is found
                End If
            Else
                Debug.Print "no items found."
            End If
        Next i
    End If
    End Sub
                
    
    Private Sub SpecialMkDir(ByVal path As String)
    Dim var As Variant, p As String
    Dim i As Integer
    var = Split(path, "")
    On Error Resume Next
    For i = 0 To UBound(var) - 1
        p = p & var(i)
        VBA.MkDir p
        p = p & ""
    Next
    End Sub

  2. #2
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location
    Could anybody provide any input on this?

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Try removing the below line, as it is exiting the loop of emails after the first attachment:
    Exit For ' Exit when the first is found
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location
    Quote Originally Posted by georgiboy View Post
    Try removing the below line, as it is exiting the loop of emails after the first attachment:
    Exit For ' Exit when the first is found
    Thanks for this @georgiboy

    I am still getting the error code though, it doesn't seem to like the SaveAsFile function.

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Have you tried:
    mi.Attachments.SaveAsFile trg & "\" & mi.Attachments.Filename
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location
    Quote Originally Posted by georgiboy View Post
    Have you tried:
    mi.Attachments.SaveAsFile trg & "\" & mi.Attachments.Filename
    Thanks for this.

    Yes I have just tried this but I am still getting the runtime error 438. Debugging on this line.

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Try as below:
    Private Sub CommandButton1_Click()
        Dim ol As Object 'Outlook.Application
        Dim Ns As Object 'Outlook.Namespace
        Dim i As Object
        Dim mi As Object 'Outlook.MailItem
        Dim inboxFol As Object 'Outlook.Folder
        Dim colItems As Object 'Outlook.Items
        Dim strFilter As String
        Dim resItems As Object
        Set ol = CreateObject(Class:="Outlook.Application")
        Set Ns = ol.GetNamespace("MAPI")
        Set inboxFol = Ns.GetDefaultFolder(6) 'olFolderInbox
        Set colItems = inboxFol.Items
        colItems.Sort "[SentOn]", False ' oldest to newest
        strFilter = "[SentOn]>'" & Format(Date, "DDDDD HH:NN") & "'"
        Debug.Print "strFilter .....: " & strFilter
        Set resItems = colItems.Restrict(strFilter)
        Debug.Print "resItems.Count: " & resItems.Count
        If resItems.Count Then
            For Each i In resItems
                If i.Class = 43 Then
                    Set mi = i
                    If mi.attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
                        Debug.Print "Subject.....: " & mi.Subject
                        Debug.Print "SentOn .....: " & mi.SentOn
                        Dim trg As String
                        trg = "C:\attachments"
                        With CreateObject("Scripting.FileSystemObject")
                            If Not .FolderExists(trg) Then .CreateFolder trg
                        End With
                        mi.attachments.Item(1).SaveAsFile trg & "\" & mi.attachments.Item(1).Filename
                    End If
                Else
                    Debug.Print "no items found."
                End If
            Next i
        End If
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location
    Quote Originally Posted by georgiboy View Post
    Try as below:
    Private Sub CommandButton1_Click()
        Dim ol As Object 'Outlook.Application
        Dim Ns As Object 'Outlook.Namespace
        Dim i As Object
        Dim mi As Object 'Outlook.MailItem
        Dim inboxFol As Object 'Outlook.Folder
        Dim colItems As Object 'Outlook.Items
        Dim strFilter As String
        Dim resItems As Object
        Set ol = CreateObject(Class:="Outlook.Application")
        Set Ns = ol.GetNamespace("MAPI")
        Set inboxFol = Ns.GetDefaultFolder(6) 'olFolderInbox
        Set colItems = inboxFol.Items
        colItems.Sort "[SentOn]", False ' oldest to newest
        strFilter = "[SentOn]>'" & Format(Date, "DDDDD HH:NN") & "'"
        Debug.Print "strFilter .....: " & strFilter
        Set resItems = colItems.Restrict(strFilter)
        Debug.Print "resItems.Count: " & resItems.Count
        If resItems.Count Then
            For Each i In resItems
                If i.Class = 43 Then
                    Set mi = i
                    If mi.attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
                        Debug.Print "Subject.....: " & mi.Subject
                        Debug.Print "SentOn .....: " & mi.SentOn
                        Dim trg As String
                        trg = "C:\attachments"
                        With CreateObject("Scripting.FileSystemObject")
                            If Not .FolderExists(trg) Then .CreateFolder trg
                        End With
                        mi.attachments.Item(1).SaveAsFile trg & "\" & mi.attachments.Item(1).Filename
                    End If
                Else
                    Debug.Print "no items found."
                End If
            Next i
        End If
    End Sub
    Wow, thank you so much! This worked a treat. I can't really figure out where I was going wrong :/

    Could I please be annoying and ask for a little more help with this?

    Is there a way I could get the script to download all attachments sent today, from the specified email address, and fill in a activeX list box with the downloaded attachments?

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    sent today
    Should be taken care of with the below line:
    strFilter = "[SentOn]>'" & Format(Date, "DDDDD HH:NN") & "'"
    from the specified email address
    Should be taken care of with the below line (it's looking for Chargehand in the SenderName):
    If mi.attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
    and fill in a activeX list box with the downloaded attachments?
    Done on the attachment
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    If you want to change it to be from a specific email adress instead of name then maybe change this line:
    If mi.attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
    To:
    If mi.attachments.Count > 0 And mi.senderemailaddress = "blah@blah.com" Then
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  11. #11
    VBAX Regular
    Joined
    Mar 2023
    Posts
    28
    Location
    Quote Originally Posted by georgiboy View Post
    If you want to change it to be from a specific email adress instead of name then maybe change this line:
    If mi.attachments.Count > 0 And InStr(mi.SenderName, "Chargehand") Then
    To:
    If mi.attachments.Count > 0 And mi.senderemailaddress = "blah@blah.com" Then
    Thank you so much for this, you are a lifesaver.

  12. #12
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    No problem, happy to help.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    ecalid, if you are happy with the results here can you mark the thread Solved please?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •