dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Save Attachments from Current Open Outlook Email

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Resolved [RESOLVED] Save Attachments from Current Open Outlook Email

    Hi Guys,

    I'm having a Brain ache trying to figure this out..

    I have the below code that searches through my Inbox and saves attachments from all emails that meeting the restricted criteria, this works fine.

    What I am having trouble with is trying to adapt the code so that it only saves attachments from the current open email. Any help on this would be greatly appreciated.

    Code:
    Sub Find_and_Save() ' finds email, saves attachment
         
        Dim olMail As Object, olAtt As Object, pdat As Date
        Dim strSaveToFolder As String, strPathAndFilename As String, Monday As String, prevpdat As String
       
         pdat = Format(Now, "dd/mm/yyyy")
     
            prevpdat = Format(pdat - 1, "ddddd") & " 23:59"
    strSaveToFolder = "P:\H925 Buying\Data Trading Administration forms\Temp Folder\"
        On Error GoTo errorhandler
        With CreateObject("Outlook.Application").GetNamespace("MAPI")
       For Each olMail In .GetDefaultFolder(6).Items.Restrict("[ReceivedTime] > '" & prevpdat & "'")
             
                    If olMail.Attachments.Count > 0 Then
                        For Each olAtt In olMail.Attachments
                        Application.DisplayAlerts = False
                            strPathAndFilename = strSaveToFolder & Format(pdat, "dd.mm.yyyy") & " " & olAtt.Filename
                                
                                olAtt.SaveAsFile strPathAndFilename
                                olMail.Save
                                Application.DisplayAlerts = True
                        Next olAtt
                    End If
           Next
                  On Error GoTo 0
        End With
    errorhandler:
         
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,819

    Re: Save Attachments from Current Open Outlook Email

    see if this will get you started
    Code:
    Dim msg As MailItem
    Set msg = ActiveInspector.CurrentItem
    For Each a In msg.Attachments
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Re: Save Attachments from Current Open Outlook Email

    Quote Originally Posted by westconn1 View Post
    see if this will get you started
    Code:
    Dim msg As MailItem
    Set msg = ActiveInspector.CurrentItem
    For Each a In msg.Attachments
    Sorry, this code resulted on an error on the first line, the error was 'User-defined type not defined'

    Just in case I didnt make it clear on my original post, I'm running the code in Excel VBA, not through the Outlook VBA, I believe the code given may be for Outlook VBA.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,819

    Re: Save Attachments from Current Open Outlook Email

    the error was 'User-defined type not defined'
    yes of course mailitem is an outlook object, to use without a reference to outlook you would need to change to object

    Code:
        With CreateObject("Outlook.Application").GetNamespace("MAPI")
                        Set msg = .ActiveInspector.CurrentItem
                        For Each a In msg.Attachments
    a would also need to be an object
    in fact you do mot need an object for msg as you could just do like
    Code:
        For Each a in CreateObject("Outlook.Application").GetNamespace("MAPI").ActiveInspector.CurrentItem.Attachments
    only saves attachments from the current open email
    i assumed that as you had existing code you would only need the pointer to activeinspector
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Re: Save Attachments from Current Open Outlook Email

    Quote Originally Posted by westconn1 View Post
    yes of course mailitem is an outlook object, to use without a reference to outlook you would need to change to object

    Code:
        With CreateObject("Outlook.Application").GetNamespace("MAPI")
                        Set msg = .ActiveInspector.CurrentItem
                        For Each a In msg.Attachments
    a would also need to be an object

    in fact you do mot need an object for msg as you could just do like
    Code:
        For Each a in CreateObject("Outlook.Application").GetNamespace("MAPI").ActiveInspector.CurrentItem.Attachments

    i assumed that as you had existing code you would only need the pointer to activeinspector

    I tried both sets of code both with 'a' and 'msg' declared as Object Variables and both resulted in the below error message;

    'Object doesn't support this property or method' this appeared on the code line contain msg on the first set of code, and on the entire line of the second set of code.

    Yes, I only need the reference to the current open email as I have the rest of the code to extract and save the attachments.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Re: Save Attachments from Current Open Outlook Email

    Quote Originally Posted by westconn1 View Post
    yes of course mailitem is an outlook object, to use without a reference to outlook you would need to change to object

    Code:
        With CreateObject("Outlook.Application").GetNamespace("MAPI")
                        Set msg = .ActiveInspector.CurrentItem
                        For Each a In msg.Attachments
    a would also need to be an object

    in fact you do mot need an object for msg as you could just do like
    Code:
        For Each a in CreateObject("Outlook.Application").GetNamespace("MAPI").ActiveInspector.CurrentItem.Attachments

    i assumed that as you had existing code you would only need the pointer to activeinspector

    I tried both sets of code both with 'a' and 'msg' declared as Object Variables and both resulted in the below error message;

    'Object doesn't support this property or method' this appeared on the code line contain msg on the first set of code, and on the entire line of the second set of code.

    Yes, I only need the reference to the current open email as I have the rest of the code to extract and save the attachments.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Re: Save Attachments from Current Open Outlook Email

    Im using Excel 2016 32bit if it matters..

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,819

    Re: Save Attachments from Current Open Outlook Email

    my error, copying from your original code, you should not reference the namespace to return the active inspector
    Code:
     For Each a In CreateObject("Outlook.Application").activeinspector.currentitem.attachments
            MsgBox a.DisplayName
        Next
    i ntested this in excel 2013 (latest i got), worked correctly without error
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    164

    Re: Save Attachments from Current Open Outlook Email

    Quote Originally Posted by westconn1 View Post
    my error, copying from your original code, you should not reference the namespace to return the active inspector
    Code:
     For Each a In CreateObject("Outlook.Application").activeinspector.currentitem.attachments
            MsgBox a.DisplayName
        Next
    i ntested this in excel 2013 (latest i got), worked correctly without error
    That works perfectly. Thank you.

    Below is my finished Code and does the Job I requested and extracts any attachments from the last viewed open Outlook email.

    Code:
    Sub Find_and_Save() ' finds email, saves attachment
         
        Dim olAtt As Object
        Dim strSaveToFolder As String, strPathAndFilename As String
       Dim a As Object
         
    strSaveToFolder = "P:\H925 Buying\Data Trading Administration forms\Temp Folder\"
       
        For Each olAtt In CreateObject("Outlook.Application").activeinspector.currentitem.attachments
            MsgBox olAtt.DisplayName
            strPathAndFilename = strSaveToFolder & olAtt.Filename
            Application.DisplayAlerts = False
            olAtt.SaveAsFile strPathAndFilename
            Application.DisplayAlerts = True
        Next
    
    End Sub

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width