VBA to measure response time for each emails in a shared mailbox

Status
Not open for further replies.

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Hi All,
i have put together a macro to measure reponse time taken for an email response in a shared emailbox.


it works fine in picking all the items from inbox but fails to pick any email sent by users. Email goes from personal inbox. Here is the macro for understanding. Can anyone please rectify so that all responses do get picked as well.

Option Explicit

Public ns As Outlook.Namespace

Private Const EXCHIVERB_REPLYTOSENDER = 102
Private Const EXCHIVERB_REPLYTOALL = 103
Private Const EXCHIVERB_FORWARD = 104

Private Const PR_LAST_VERB_EXECUTED = "http://schemas.microsoft.com/mapi/proptag/0x10810003"
Private Const PR_LAST_VERB_EXECUTION_TIME = "http://schemas.microsoft.com/mapi/proptag/0x10820040"
Private Const PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Private Const PR_RECEIVED_BY_ENTRYID As String = "http://schemas.microsoft.com/mapi/proptag/0x003F0102"

Private Function GetReply(olMailItem As MailItem) As MailItem

On Error Resume Next

Dim conItem As Outlook.Conversation
Dim ConTable As Outlook.Table
Dim ConArray() As Variant
Dim MsgItem As MailItem
Dim lp As Long
Dim LastVerb As Long
Dim VerbTime As Date
Dim Clockdrift As Long
Dim OriginatorID As String

Set conItem = olMailItem.GetConversation
OriginatorID = olMailItem.PropertyAccessor.BinaryToString(olMailItem.PropertyAccessor.GetProperty(PR_RECEIVED_BY_ENTRYID))

If Not conItem Is Nothing Then
Set ConTable = conItem.GetTable
ConArray = ConTable.GetArray(ConTable.GetRowCount)
LastVerb = olMailItem.PropertyAccessor.GetProperty(PR_LAST_VERB_EXECUTED)
Select Case LastVerb
Case EXCHIVERB_REPLYTOSENDER, EXCHIVERB_REPLYTOALL, EXCHIVERB_FORWARD
VerbTime = olMailItem.PropertyAccessor.GetProperty(PR_LAST_VERB_EXECUTION_TIME)
VerbTime = olMailItem.PropertyAccessor.UTCToLocalTime(VerbTime)
Debug.Print "Reply to " & olMailItem.Subject & " sent on (local time): " & VerbTime
For lp = 0 To UBound(ConArray)
If ConArray(lp, 4) = "IPM.Note" Then
Set MsgItem = ns.GetItemFromID(ConArray(lp, 0))
If Not MsgItem.Sender Is Nothing Then
If OriginatorID = MsgItem.Sender.ID Then
Clockdrift = DateDiff("s", VerbTime, MsgItem.SentOn)
If Clockdrift >= 0 And Clockdrift < 300 Then
Set GetReply = MsgItem
Exit For
End If
End If
End If
End If
Next
Case Else
End Select
End If

End Function

Public Sub ListIt()
Dim myOlApp As New Outlook.Application
Dim myItem As Object
Dim myReplyItem As Outlook.MailItem
Dim myFolder As Folder
Dim xlRow As Long
Dim olsharename as outlook.Mailitem


Set ns = myOlApp.GetNamespace("MAPI")
Set olsharename = createrecipient("Mailbox name")
Set myFolder = ns.GetSharedDefaultFolder(olSharename,olFolderInbox)


xlRow = 3
For Each myItem In myFolder.Items
If myItem.Class = olMail Then
Set myReplyItem = GetReply(myItem)
If Not myReplyItem Is Nothing Then
PopulateSheet ActiveSheet, myItem, myReplyItem, xlRow
xlRow = xlRow + 1

Else: PopulateSheet ActiveSheet, myItem, myReplyItem, xlRow
xlRow = xlRow + 1
End If
End If
DoEvents
Next
MsgBox "Done"

End Sub


Private Sub PopulateSheet(mySheet As Worksheet, myItem As MailItem, myReplyItem As MailItem, xlRow As Long)

On Error Resume Next

Dim recips() As String
Dim Recipients As Outlook.Recipient
Dim lp As Long

With mySheet
.Cells(xlRow, 1).FormulaR1C1 = myItem.Sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
.Cells(xlRow, 2).FormulaR1C1 = myItem.Subject
.Cells(xlRow, 3).FormulaR1C1 = myItem.ReceivedTime
.Cells(xlRow, 4).FormulaR1C1 = myItem.Categories
.Cells(xlRow, 5).FormulaR1C1 = myReplyItem.Sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
For lp = 0 To myReplyItem.Recipients.Count - 1
ReDim Preserve recips(lp) As String
recips(lp) = myReplyItem.Recipients(lp + 1).Address
Next
.Cells(xlRow, 6).FormulaR1C1 = myReplyItem.To
.Cells(xlRow, 7).FormulaR1C1 = myReplyItem.CC
.Cells(xlRow, 8).FormulaR1C1 = myReplyItem.Subject
.Cells(xlRow, 9).FormulaR1C1 = myReplyItem.SentOn

If .Cells(xlRow, 5).Value = "" Then

.Cells(xlRow, 11).FormulaR1C1 = "=Now()-RC[-8]"
.Cells(xlRow, 11).NumberFormat = "[h]:mm:ss"

Else
.Cells(xlRow, 10).FormulaR1C1 = "=RC[-1]-RC[-7]"
.Cells(xlRow, 10).NumberFormat = "[h]:mm:ss"

End If
End With
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This part:
Set olsharename = createrecipient("Mailbox name")
Set myFolder = ns.GetSharedDefaultFolder(olSharename,olFolderInbox)

is using the inbox. You also need to look in the sent folder. Since its only one folder and about 10 lines, you could repeat the section that does the inbox (From Set myfolder... ending with Next) and change it to the sent folder.
 

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
HI,

Not sure I understand correctly.

Responses are sent from personal inbox of respective users and thus it does not even appear on sent items of shared emailbox.

Somehow the solution must look at the thread and identify the responses for all such users who send responses.

Or

Emails sent by users should somehow get listed under shared email box instead of their personal inbox.

Regards
Karthik
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You can set Exchange to use the shared sent folder... or use the getdefaultfolder code.

As for linking the items, do that in Excel using the subject lines and compare the last_verb times with the sent time - but if all you need is the response time, last verb values of 102 or 103 will have the reply/reply all times.
 

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Thank you.

yeah , i tried setting outlook to send emails from shared mailbox. Currently, emails I send outta shared mailbox are stuck in outbox. I tried troubleshooting using online methods. So far i am not succesful

So far I taken care of the below :
DelegateSentItemsStyle registry value set to 0
Cached echange mode

would you know any other reason why this is happening,

cheers
 

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
I think IT admin should be running those cmdlets and i have escalated it to them. Thanx.

On a different note, i want to pick emails from multiple folders within Inbox.

Set ns = myOlApp.GetNamespace("MAPI")
Set olsharename = createrecipient("Mailbox name")
Set myFolder = ns.GetSharedDefaultFolder(olSharename,olFolderInbox)

could you send an adjusted code with, lets say folders as "Invoices" & "Test". Lets assume these folders are subfolders within Inbox.

It would be highly helpful, thank you.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
to watch two folders you need to use something like this:

i = 1 to 2
Set myFolder = ns.GetSharedDefaultFolder(olSharename,olFolderInbox)

select case i
case 1
Set myFolder= myfolder.folder("Invoices")
case 2
set myFolder= myfolder.folder("test")
end select

xlRow = 3
For Each myItem In myFolder.Items
If myItem.Class = olMail Then
Set myReplyItem = GetReply(myItem)
If Not myReplyItem Is Nothing Then
PopulateSheet ActiveSheet, myItem, myReplyItem, xlRow
xlRow = xlRow + 1

Else: PopulateSheet ActiveSheet, myItem, myReplyItem, xlRow
xlRow = xlRow + 1
End If
End If
DoEvents
Next
next i
 

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Unfortunately, it still does not pick from both the folders. It is picking only either or those.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Any error messages? If you use Step Into and watch it, does it hit each folder? Oh... i'll bet it's the xlRow = 3 - move that line above the select case. It's probably going back to row 3 for the second folder.
 

Kartyk

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
actually, it was for a different code i was working on... although I would want the same feature in both the codes. No error messages as such, just that I find data only from one of the folders. May be it is overriding data..not sure



Heres the one I tested now.




Dim olApp As Outlook.Application

Dim NS As Outlook.Namespace

Dim olSharename As Outlook.Recipient

Dim Folder As Outlook.Folder




Set olApp = Outlook.Application

Set NS = olApp.GetNamespace("MAPI")

Set olSharename = NS.CreateRecipient("otc.brokerage@credit-suisse.com")


For j = 1 To 2


Set Folder = NS.GetSharedDefaultFolder(olSharename, olFolderInbox)


Select Case j


Case 1

Set Folder = Folder.Folders("Invoices")


Case 2


Set Folder = Folder.Folders("Sneha")


End Select


Next


Set Items = Folder.Items



Sheets("Data").Visible = True

Sheets("Sheet2").Visible = True


Sheets("Data").Range("C2:H100000").ClearContents


With ActiveWorkbook.Sheets("Data").Select

Range("c" & 1).Value = "Recieved time"

Range("e" & 1).Value = "Sender"

Range("f" & 1).Value = "Subject"

Range("g" & 1).Value = "Categories"


End With


For i = 1 To Items.Count

Set myitem = Items(i)

msgtext = myitem.Body


Range("c" & i + 1).Value = myitem.ReceivedTime

Range("e" & i + 1).Value = myitem.SenderName

Range("f" & i + 1).Value = myitem.Subject

Range("g" & i + 1).Value = myitem.Categories



Next i






==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer:
Electronic Communications Disclaimer - Credit Suisse
==============================================================================
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
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
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
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
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
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
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 3
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2

Similar threads

Top