VBA Script - Print Date between first email in Category X and last email in Category Y

AdamFuller

Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Part of my workflow revolves around hitting certain deadlines. I'd love to have a dialogue pop up when my rules categorize the last piece of the process showing: the number of days between the first email received in "09) Purchase Agreement Received (F2)" and the most recent email received in "00) Final CD (F11)". I already have a rule in place to run the following script to Categorize the email and am not sure how to proceed in VBA to get the desired result. Thank in advance for any light you guys can help shed on this!

Sub FlagFCD(olItem As MailItem)
Dim olAtt As Attachment
If olItem.Attachments.Count > 0 Then
For Each olAtt In olItem.Attachments
If LCase(olAtt.FileName) Like "*.pdf" Then 'If attachment type is PDF
olItem.Categories = "00) Final CD (F11)" 'Categorize Emails with PDF Attachments as 00) Final CD (F11)
olItem.Close olSave
Exit For
End If
Next olAtt
End If
lbl_Exit:
Exit Sub
End Sub

1617900969832.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
If that is the last - you'd need to get the subject (assuming the subject is the same) - or use purchase order # or some other unique value so you can find the first message, using the category and the unique keyword to find it. Get the sent date and calculate.
 

AdamFuller

Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Well, it took some grinding, but this is what I came up with. It relies on a manual Macro button. The only way to search this was once the categorized email manually transferred the correct clients folder. I'm sure the code is a mess, but I couldn't be happier with the results. Hope it helps some others out!

Sub LoanUpdate()

Dim Inbox As Outlook.MAPIFolder
Set Inbox = Application.ActiveExplorer.CurrentFolder

Dim AllCurrentEmails As Outlook.Items
Set AllCurrentEmails = Inbox.Items
AllCurrentEmails.Sort "[ReceivedTime]", False

On Error Resume Next

Set AllCurrentEmails = Inbox.Items.Restrict("[Categories] = '09) Purchase Agreement Received (F2)'")

Dim EmailOne As Object
Set EmailOne = AllCurrentEmails.GetFirst

Dim Start As String
Start = Format(EmailOne.ReceivedTime, "mm/dd/yyyy")

Set AllCurrentEmails = Inbox.Items.Restrict("[Categories] = '00) Final CD (F11)'")

Dim EmailTwo As Object
Set EmailTwo = AllCurrentEmails.GetLast

Dim Finish As String
Finish = Format(EmailTwo.ReceivedTime, "mm/dd/yyyy")

Dim TTC As String
TTC = (DateDiff("d", Start, Finish) + 1)

Dim TimeElapsed As String
TimeElapsed = (DateDiff("d", Start, Date) + 1)

If Start = "" And Finish = "" Then MsgBox "No Loan started yet", , "Loan Update"
If Start = "" And Finish <> "" Then MsgBox "No Purchase Agreement Flagged!", , "Loan Update"
If Start <> "" And Finish = "" Then MsgBox "PA Received: " & Start & " (" & TimeElapsed & " days ago)", , "Loan Update"
If Start <> "" And Finish <> "" Then MsgBox "PA Received: " & Start & " | " & "CTC Received: " & Finish & vbCrLf & vbCrLf & "Loan Closed in " & TTC & " days", , "Loan Update"

End Sub

Example outputs;

1617948406566.png
1617948436643.png
1617948479398.png
1617948608421.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'm sure the code is a mess,
It doesn't look like a mess.

Typically, DIM statements are all put at the top but it's not unusual to add some right before they are used. That's as nit-picky as it can get. :)

Good job. ??

(hmmm no clapping smiles in the forum... will need to use the ones from windows)
(ETA: and windows emojis don't work in the forum :( )
(ETA2: had to upgrade the database to support unicode.... now emojis work. ? for me too. :))
 
Similar threads
Thread starter Title Forum Replies Date
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
D.Moore VBA script fail after Office 365 update Using Outlook 8
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
R VBA Script Quick Parts Using Outlook 1
Q VBA Script to move item in secondary mailbox Outlook VBA and Custom Forms 2
N VBA Script to Send Automatic Emails from Outlook 2010 Outlook VBA and Custom Forms 1
O modify vba to run it as script rule Outlook VBA and Custom Forms 8
P How many subs can run in one outlook VBA script Using Outlook 5
J Email Parsing VBA Script for Outlook - NEEDED Outlook VBA and Custom Forms 7
P Vba script including macro appears in rules but wont run Outlook VBA and Custom Forms 6
R Adding vba to script list Outlook VBA and Custom Forms 4
F VBA script to highlight specific words Outlook VBA and Custom Forms 1
D VBA Script to extract text matching specific criteria Outlook VBA and Custom Forms 1
D VBA Script (Ask to where to save send mail) Outlook VBA and Custom Forms 1
M VBA script to allow mail merges of distribution groups? Using Outlook 7
D VBA script to auto download attachments and rename file according to subject line Outlook VBA and Custom Forms 23
Hudas Outlook VBA script reverting back to previous changes Outlook VBA and Custom Forms 2
J Outlook 2007 Rules & VBA: How to run a script on a report message (ReportItem) Using Outlook 14
V "Accept + Send the Response now", VBA script? Using Outlook 1
R Addins4Outlook TagIt! addin script or VBA module? Using Outlook 2
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
A VBA Script to Forward Spam to AntiSpam Provider Using "Blank" Form Outlook VBA and Custom Forms 2
L Limit VBA Script to one Outlook account Using Outlook 1
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 0
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0

Similar threads

Top