I'm trying to automate a workflow, where necessary information gets sent to my email while I'm responding to an alarm call, then excel takes the body of that email and parses it into individual cells.
After two weeks of researching, I've found the beginnings, but I'm hoping for something more concise.
This is my code so far:
Sub OutlookExtract()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Alarms")
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("From_date").Value Then
Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
My issue is, that while it does technically include the email body, I have to follow it up with more Excel syntax in order to further parse the info into something useful.
I have to code multiple cells with MID
LEN
in order to pull my times out, and then run an entirely different code to paste those values in where I actually need them to go:
Sub ValuePaste()
Range("B3").Value = Range("N3")
Range("B4").Value = Range("N4")
Range("B5").Value = Range("N5")
Range("B6").Value = Range("N6")
Range("B7").Value = Range("N7")
Range("B10").Value = Range("N8")
Range("G29").Value = Range("N9")
Range("B11").Value = Range("N10")
Range("O11").Value = Range("N11")
Range("B13").Value = Range("N12")
Range("B14").Value = Range("N13")
Range("B16").Value = Range("N14")
End Sub
This is the table I have on my Inputs sheet (A), Sub ValuePaste()
uses the table on the right to paste the values from sheet (B) into my inputs
Honestly, I'm fine running the two codes, and I know that if I figured out how to properly parse the body then I'd be able to combine them, but as it is now, I have to run the first code, check that none of my MID
LEN
's have errored/have the correct data, and then run the second code.
I'm trying to automate this in order to cut down on human error, but it feels like it's just adding more opportunity for me to mess something up.
Please, try the next function. It will return a 2D array having in the first row the necessary headers and in the second the extracted corespondent values:
It must be used in the next way: