Consulting

Results 1 to 5 of 5

Thread: Find and Open an email in Outlook

  1. #1
    VBAX Contributor
    Joined
    Jun 2008
    Location
    West Midlands
    Posts
    170
    Location

    Find and Open an email in Outlook

    Guys,

    I have the below code, which works, it finds a specific email, and saves the attachment on the email to another folder in explorer.

    The problem I have is that each day the email name will change in respect that the Date and Time will change.

    How can I modify the line of code that has the name of the subject name of the email so that it looks for key words rather than the entire and full subject name?

    Option Explicit
    Sub SaveFundAttachments()
        Dim olApp As Outlook.Application
        Dim olNS As Outlook.Namespace
        Dim olInBox As Outlook.MAPIFolder
        Dim olMoveToFolder As Outlook.MAPIFolder
        Dim olItems As Outlook.Items
        Dim olAtt As Outlook.Attachment
        Dim strSaveToFolder As String
        Dim strPathAndFilename As String
        Dim Ans As Long
        Dim i As Long
        
        strSaveToFolder = "I:\H904 Supply Chain\Scott Atkinson\Dashboard\"
        
        If Right(strSaveToFolder, 1) <> "\" Then strSaveToFolder = strSaveToFolder & "\"
        
        Set olApp = CreateObject("Outlook.Application")
        
        Set olNS = olApp.GetNamespace("MAPI")
        Set olInBox = olNS.GetDefaultFolder(olFolderInbox)
        
        ' ***** How can I amend the below line to search for part of the Subject name and not all of it *****
        Set olItems = olInBox.Items.Restrict("[Subject] = 'Availability Measurement by SKU was executed at 23/01/2016 10:31:56'")
        
        
        For i = olItems.Count To 1 Step -1
            If olItems(i).Attachments.Count > 0 Then
                For Each olAtt In olItems(i).Attachments
                    strPathAndFilename = strSaveToFolder & olAtt.Filename
                    If Len(Dir(strPathAndFilename, vbNormal)) = 0 Then
                        olAtt.SaveAsFile strPathAndFilename
                        olItems(i).Save
                    Else
                        Ans = MsgBox(olAtt.Filename & " already exists.  Overwrite file?", vbQuestion + vbYesNo)
                        If Ans = vbYes Then
                            olAtt.SaveAsFile strPathAndFilename
                            olItems(i).Save
                        End If
                    End If
                Next olAtt
               
            End If
        Next i
        
        Set olApp = Nothing
        Set olNS = Nothing
        Set olInBox = Nothing
        Set olMoveToFolder = Nothing
        Set olItems = Nothing
        Set olAtt = Nothing
        
    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ?
    Sub vbax_54989_FindEmailsWithSpecificSubject()
        
        Dim olMail As Object, olAtt As Object
        Dim strSaveToFolder As String, strPathAndFilename As String
        
        strSaveToFolder = "I:\H904 Supply Chain\Scott Atkinson\Dashboard\"
    
        With CreateObject("Outlook.Application")
            For Each olMail In .GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
                If InStr(olMail.Subject, "Availability Measurement by SKU was executed") > 0 Then
                    If olMail.Attachments.Count > 0 Then
                        For Each olAtt In olMail.Attachments
                            strPathAndFilename = strSaveToFolder & olAtt.Filename
                            If Len(Dir(strPathAndFilename, vbNormal)) = 0 Then
                                olAtt.SaveAsFile strPathAndFilename
                                olMail.Save
                            Else
                                If MsgBox(olAtt.Filename & " already exists.  Overwrite file?", vbQuestion + vbYesNo) = vbYes Then
                                    olAtt.SaveAsFile strPathAndFilename
                                    olMail.Save
                                End If
                            End If
                        Next olAtt
                    End If
                End If
            Next
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Contributor
    Joined
    Jun 2008
    Location
    West Midlands
    Posts
    170
    Location
    Thank you. This code works perfectly.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. thanks for the feedback.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Outlook has some builtin find/filter tools.

    Sub M_snb()
       On Error Resume Next
      c00 ="Availability Measurement by SKU was executed"
    
      With CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6).Items
         for each at in .Find("[Subject]='" & c00 & "'").attachments
          at.saveasfile "I:\H904 Supply Chain\Scott Atkinson\Dashboard\" & at.filename
        next
      End With
    End Sub
    read more:

    http://www.snb-vba.eu/VBA_Outlook_external_en.html#L161

Posting Permissions

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