Save Emails from Excel

Joshyd

New Member
Joined
Mar 6, 2019
Messages
17
Hi There,

I have a new specific problem with interacting with outlook.

I have a sheet where I need to save approval for things that came via email.

I will be using a userform to allow my users to select which row the approval should go in.

I need the macro to look at the user's inbox (there are multiple users, so it needs to be dynamic) and search based on an entered criteria (I am thinking an InputBox is best for this, but if you have a better idea, that's cool)

I would like the macro to then open the email and allow the user to confirm that it is the right email.

Should it be the right email, (If not, I would like it to go back to enter a new criteria to search for), I would like it to be saved to a specific path.

I would also like it to hyperlink to a cell based on the row in question with the text "Approval".


Flow example:

  1. A userform has a ComboBox listing the different rows
  2. The user Clicks a Button, which launches an InputBox
  3. The box asks what the subject of the email is
  4. Depending on the case, the user's inbox is then searched for the email
  5. (I am not sure if there is a way to generate a list of the matching emails, or maybe if there should be a date added to step 3, I just worry about duplicate subjects.
  6. The email is opened and an InputBox pops up to confirm whether it is the right email
  7. If it is the correct email, then proceed to step 9
  8. If it is the wrong email, then proceed to step 3
  9. The email is then saved to a specific file path, with a specific name (based on other values in the row selected in step 1)
  10. In the sheet, the cell in the column labelled "Approval", and the row from step 1, needs to say "Approval" and be hyperlinked to the email based on the name from step 9

What I have:


  • The userform row is based on a dynamic range called "Role_Job_Title" and is valued at =OFFSET(INDIRECT('Sheet Back End'!J1&"E3"),0,0,COUNTA(INDIRECT('Sheet Back End'!J1&"$E:$E"))-1,1)
  • This is the some of the sample code I have from searching a calendar, but I cannot figure out how to edit it:

VBA Code:
Function getLocation() As String
    Dim oApp As Outlook.Application, oCal As Outlook.Folder, appt As Outlook.AppointmentItem
    Dim explorerInstance As Outlook.Explorer, objNS As Outlook.Namespace
    Dim ofldItems As Outlook.Items, sortItems As Outlook.Items
    Dim tdystart As Date, tdyend As Date
    Dim sRestrict As String
    Dim myRecipient As Outlook.Recipient
    
    tdystart = DateSerial(Year(Range("U4")), Month(Range("U4")), Day(Range("U4")))
    tdyend = DateSerial(Year(Range("U4")), Month(Range("U4")), Day(Range("U4")) + 2)
    Set oApp = CreateObject("Outlook.application")
    Set objNS = oApp.GetNamespace("MAPI")
    Set myRecipient = objNS.CreateRecipient("User")
    myRecipient.Resolve
       
    If myRecipient.Resolved Then
        Set oCal = objNS.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
    End If
    
    If oCal Is Nothing Then
        For Each explorerInstance In oApp.Explorers
            If InStr(1, explorerInstance.Caption, "Calendar") > 0 Then
                Set oCal = explorerInstance.CurrentFolder
                Exit For
            End If
        Next
        If oCal Is Nothing Then
            Exit Function
        End If
    End If
    
    Set ofldItems = oCal.Items
    ofldItems.Sort ("[Start]")
    sRestrict = "[Start] >= '" & tdystart & "' and [End] <= '" & tdyend & "'"
    Set sortItems = ofldItems.Restrict(sRestrict)
    sortItems.Sort ("[Subject]")
    sortItems.IncludeRecurrences = True
    
    Dim myRecurrPatt As Outlook.RecurrencePattern
    For Each appt In sortItems
        If appt.Subject = "Onboarding: Matrix Welcome and Office Orientation Placeholder" Then
            If Weekday(appt.Start) = 2 Then
                tdystart = DateSerial(Year(Range("U4")), Month(Range("U4")), Day(Range("U4")))
                Else
                    If Weekday(appt.Start) = 3 Then
                        tdystart = DateSerial(Year(Range("U5")), Month(Range("U5")), Day(Range("U5")))
                        Else
                            If Weekday(appt.Start) = 4 Then
                                tdystart = DateSerial(Year(Range("U6")), Month(Range("U6")), Day(Range("U6")))
                                Else
                                  tdystart = ""
                            End If
                    End If
            End If
            tdystart = tdystart + TimeSerial(Hour(appt.Start), Minute(appt.Start), Second(appt.Start))
            Set myRecurrPatt = appt.GetRecurrencePattern
            
    On Error Resume Next
            Set appt = myRecurrPatt.GetOccurrence(tdystart)
            getLocation1 = appt.location
            Set appt = Nothing
            Set myRecurrPatt = Nothing
            Exit For
        End If
    Next appt
    
    Range("U7") = getLocation1
     
    Set oApp = Nothing
    Set objNS = Nothing
    Set oCal = Nothing
    Set ofldItems = Nothing
    Set sortItems = Nothing
End Function

  • I also found this code online, but I can't seem to adjust it either:
VBA Code:
Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
                                 ExtString As String, DestFolder As String)
    Dim ns As Namespace
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim MyDocPath As String
    Dim I As Integer
    Dim wsh As Object
    Dim fs As Object

    On Error GoTo ThisMacro_err

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders(OutlookFolderInInbox)

    I = 0
    ' Check subfolder for messages and exit of none found
    If SubFolder.Items.Count = 0 Then
        MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
               vbInformation, "Nothing Found"
        Set SubFolder = Nothing
        Set Inbox = Nothing
        Set ns = Nothing
        Exit Sub
    End If

    'Create DestFolder if DestFolder = ""
    If DestFolder = "" Then
        Set wsh = CreateObject("WScript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        MyDocPath = wsh.SpecialFolders.Item("mydocuments")
        DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
        If Not fs.FolderExists(DestFolder) Then
            fs.CreateFolder DestFolder
        End If
    End If

    If Right(DestFolder, 1) <> "\" Then
        DestFolder = DestFolder & "\"
    End If

    ' Check each message for attachments and extensions
    For Each Item In SubFolder.Items
        For Each Atmt In Item.Attachments
            If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
                FileName = DestFolder & Item.SenderName & " " & Atmt.FileName
                Atmt.SaveAsFile FileName
                I = I + 1
            End If
        Next Atmt
    Next Item

    ' Show this message when Finished
    If I > 0 Then
        MsgBox "You can find the files here : " _
             & DestFolder, vbInformation, "Finished!"
    Else
        MsgBox "No attached files in your mail.", vbInformation, "Finished!"
    End If

    ' Clear memory
ThisMacro_exit:
    Set SubFolder = Nothing
    Set Inbox = Nothing
    Set ns = Nothing
    Set fs = Nothing
    Set wsh = Nothing
    Exit Sub

    ' Error information
ThisMacro_err:
    MsgBox "An unexpected error has occurred." _
         & vbCrLf & "Please note and report the following information." _
         & vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    Resume ThisMacro_exit

End Sub

I appreciate all the help in advance and wealth of knowledge that this forum is!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top