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
 
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.
 
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
 
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.
 
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
 
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.
 
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
 
Unfortunately, it still does not pick from both the folders. It is picking only either or those.
 
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.
 
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
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
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
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

Similar threads

Back
Top