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

Status
Not open for further replies.

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
 
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.
 
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
 
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. :))
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
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
C VBA in "New Outlook?" Using Outlook 0
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
B Requesting VBA code to make Outlook prompt for confirmation when deleting a task? Outlook VBA and Custom Forms 4
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
N VBA Code Not Working correctly Outlook VBA and Custom Forms 1
L VBA to Triage Incoming Email Outlook VBA and Custom Forms 0
J Outlook VBA to send from Non-default Account & Data Files Outlook VBA and Custom Forms 4
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0

Similar threads

Back
Top