Outlook: count sent based on signature - vba

We send emails from a shared outlook address
And now I want to find out how many emails I answer everyday because my boss wants to give me more work but I want to get an idea of how I actually compared to my colleagues before I agree on taking more work
Wondering if it's possible to count how many sent emails we send everyday and then filter that based on our signatures
Thank you

The best and the most easiest way is to handle the ItemSend event which is fired when an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
So, each time the ItemSend event is fired we can increase the counter.
Another possible solution is to use the Find/FindNext or Restrict methods for getting the number of items that contain specific keywords in the message body. Read more about them in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
How To: Get unread Outlook e-mail items from the Inbox folder

If the signature includes something unique to the user, just search the Sent Items folder for a match in the Body property using Items.Find/FindNext or Items.Restrict.

Related

Reprocessing Outlook Undelivered Mail

I have an Exchange mailbox with a bunch of Outlook ReportItem Undelivered messages. I am attempting to reprocess the undelivered messages via an Outlook VBA script by invoking the "SendAgain" operation on the ReportItem messages. My issue is that the ReportItem does not have a send method, so I have no way of actually sending the reprocessed messages. I am using the following code to go through the messages:
Dim objApp As Outlook.Application
Dim objNameSpace As NameSpace
Dim journalAlertInbox As Folder
Dim objInspector As Inspector
Dim resendItem As ReportItem
Set objApp = CreateObject("Outlook.Application")
Set objNameSpace = objApp.GetNamespace("MAPI")
Set journalAlertInbox = objNameSpace.Stores.Item("thestore").GetDefaultFolder(olFolderInbox)
For Each folderItem In journalAlertInbox.Items
If TypeOf folderItem Is ReportItem Then
folderItem.Display
Set objInspector = folderItem.GetInspector
objInspector.CommandBars.ExecuteMso "SendAgain"
Set resendItem = Application.ActiveInspector.CurrentItem
Set objInspector = resendItem.GetInspector
''how do I send the item that is now displayed?
''resendItem.Close olSave
folderItem.Close olDiscard
End If
Next folderItem
I thought I might be able to save the displayed message as a draft, however If I uncomment the resendItem.close olSave line this results in a message in my Outlook Drafts folder of type ReportItem. I can open up the saved draft message it the Outlook GUI and click the send button, but I do not see a way to actually invoke the send operation programmatically. Examining the message in drafts shows it to be of type ReportItem, which does not have a .Send method.
How can I invoke the "Send" operation on the Report Item? I can clearly see the "Send" button, but there seems to be no programmatic way of actually clicking it.
OOM does not expose any functionality that allows to link a ReportItem object to the original MailItem, and, generally, there might not be any kind of link between the two. The best you can do is to retrieve PR_ORIGINAL_SEARCH_KEY MAPI property (or PR_REPORT_TAG, which includes both the search key and the store/Sent Items folder entry id) using ReportItem.PropertyAccess.GetProperty and try to find a matching message in the Sent Items folder. You can see these properties in OutlookSpy (I am its author).
Keep in mind that OOM does not allow to search on the binary (PT_BINARY) properties in Items.Find/Restrict.
If using Redemption is an option (I am also its author), it exposes RDOReportItem.FindOriginalItem method.
Once you have the original item, you can make a copy and try to send it again.
The ReportItem doesn't represent the original item which is failed to be sent. Also it doesn't contain any relationship with the original mail item, so you will not find any property or method available in the Outlook object model. Your existing solution looks good.
You may also try using the ReportItem.GetConversation method which obtains a Conversation object that represents the conversation to which this item belongs. So, you may try getting the previous item from the conversation, it could be the original item which has been submitted.

Send email reminder when task occurs

This code works with Outlook's Task system. I created a task that reoccurs every week on Wednesday. When the task occurs the code is suppose to send an email to "test#work.com" (not the real test email).
I have written the code in the Outlook VBA developer window.
Am I missing a step to get the code to run? The code logic is posted below.
Sub Application_Reminder(ByVal Item As Object)
Dim objPeriodicalMail As MailItem
If Item.Class = olTask Then
If InStr(LCase(Item.Subject), "Send PM Work Order Request Reminder Email") Then
Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
With objPeriodicalMail
.Subject = "REMINDER-Get Work Order Request Submitted"
.To = "Test#work.com"
.HTMLBody = "<HTML><BODY>Hello All Pm's, Please Submit your Work order request by 9:00A.M.</HTML></BODY>"
.Attachments.Add
.Importance = olImportanceHigh
.ReadReceiptRequested = True
.Send
End With
End If
End If
End Sub
To control Outlook objects from outside Outlook, you must establish a reference to the Outlook object library from the project in which you are writing code. To do this, use the References dialog box in the Visual Basic Editor in the primary application. You can then write code that returns a reference to the Outlook Application object. Through this reference, your code has access to all the objects, properties, methods, and constants defined in the Outlook type library.
Read more about that in the Automating Outlook from a Visual Basic Application article.
To set up events you need to define the source object withevents or select in the drop-down list on VBA editor and select the required event. See Using Outlook Visual Basic for Applications to Respond to Outlook Events.

Save attachments as they hit subfolder

I want to make a script rule which sends to a sub folder, marks as certain category and saves attachment (90% of time PDF) in certain g:drive folder with a file naming convention.
Sub folders:
Direct
PO
Confirmation
Delivery Notes
Invoices
Statements
Categories:
PO
Confirmation
Delivery Note
Invoice
Statement
;don't require a "Direct Category"
I'm able to use normal Outlook rules for move to folder and assign category. There are about 30 individual rules based on supplier name. etc.
Had a look at some VBA scripts. Would I need 30 different scripts to tag onto my 30 current move to folder/assign category scripts to assign the separate file name pathways?
Or can I adapt a script that knows if a email hits "Invoice" it saves to G:\My Drive\Outlook attachments\Invoices
Email hits Delivery Notes it saves toG:\My Drive\Outlook attachments\Delivery Notes
File naming Convention; not thought too much into yet.
Date & Time received email and who supplier is. Joe Bloggs Ltd 17.09.21 11.34am
Also a way of marking if any duplicate files Joe Bloggs Ltd 17.09.21 11.34am (2)
You can create a VBA macro where you could subscribe to the ItemAdd event of each folder and depending on the target folder you may run a different code. The ItemAdd event is fired when one or more items are added to the specified collection. For example:
Public WithEvents myOlItems As Outlook.Items
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
Note, an instance of the item that was added is passed as a parameter to the event handler, so you can extract the required information or attachments.

Activate Out of Office reply

I would like to automate my Out of Office based on the days that I'll be out on a biweekly basis. I don't have access to the Exchange server that hosts our Outlook.
I set an Outlook rule to send a automatic reply on Monday and apply VBA code to disable this rule when I'm in the office (with Outlook open). This is not an elegant way because the rule sends a reply to the user repeatedly every time an email is sent to me.
How can I activate my Out of Office reply on Outlook 2010 using VBA?
Here are the two resources I used:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_win10/set-up-recurring-out-of-office-auto-reply-for/71dd1fef-ba99-4a2b-be72-7d509e8848eb
https://superuser.com/questions/292426/outlook-2010-how-to-turn-out-of-office-on-automatically-when-outlook-is-closed
This is the script I have in "ThisOutlookSession" to enable/disable the rule, "HomeTime" containing my Out Of Office-like message.
Option Explicit
Private Sub Application_Quit()
SetRuleEnabled True
End Sub
Private Sub Application_Startup()
SetRuleEnabled False
End Sub
Private Sub SetRuleEnabled(ByVal bEnable As Boolean)
Dim oSession As Outlook.NameSpace
Dim oRule As Outlook.Rule
Dim oRules As Outlook.Rules
Dim oPA As Outlook.PropertyAccessor
Set oSession = Application.Session
Set oRules = oSession.DefaultStore.GetRules()
Set oPA = oSession.DefaultStore.PropertyAccessor
'*** If the Out-Of-Office is already on (eg. holidays, sick leave etc.)
'*** then it might be best to force this rule permanently off
If oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B") Then
bEnable = False
End If
For Each oRule In oRules
If oRule.Name = "HomeTime" Then
oRule.Enabled = bEnable
oRules.Save
Exit For
End If
Next
End Sub
I am assuming you've checked out Om3r's link, but I have a less elegant, if not clunky solution. I once created a VBA out of office message as a practical joke (just to avoid someone). I could not find the code, but here is a summary of what you'd need to do.
(Outlook has to be running for this to work)
Create a public boolean variable, for example OutofOffice, set by a ribbon/QAT button or on a schedule.
Create and save a draft email with your message (e.g. "I am out of the office...")
Use or set up an Inbox_ItemAdd event listener, and for each incoming email:
Reply to it,
concatenating "Auto-Reply " to the subject line
Retrieve the draft email and concatenate your draft message body to the reply body (and you'll have to figure out HTMLBody versus (non-HTML) Body. Something like:
OutMail.HTMLBody = OutMail.HTMLBody & ObjDraft.HTMLBody
or you might save your OOO message as a public static string (instead of a draft email).
Send the email
as you mentioned, you don't want to repeatedly send this to people who sent you additional emails during this period. I would probably add the sender's email address (after passing to a GetSMTPAddress type function) to an array. I'd add a IsInArray type function to check each new sender (to see if they were emailed already). This array would be erased by your procedure called when you click that control again to turn off the OOO reply.
If this worked for you, of course you could create a userform to edit the OOO message and set the schedule (day of week or specific dates).

Accessing "From:" from VBA in Outlook 2010

I have on account "me#domain.com" configured in Outlook 2010. I compose a message and open the drop-down on "From:". I select "Other email address..." and type in "bingo#bongo.com". I get a pop-up asking whether to send messages from "bingo#bongo.com" via the "me#domain.com" account. I "ok" that. When I now send a message to "someone#domain.com", the recipient sees
From: bingo#bongo.com
Curiously, when I go to inspect me#domain.com/Sent Items, all I see is
From: me#domain.com
I am very frustrated about this behaviour because I wish to move sent items depending on the sending account. Initially I looked into creating a Send Rule. Irritatingly, there is no option to action anything based on "From:". So I dive into VBA. I got most of the code to move the stuff, but when I look through the mail item object (in the Locals window), I cannot find any property that states "bingo#bongo.com".
Can anyone advise how to extract the Reply-To (I guess it is) address from the outgoing mail item?
It is quite bizarre that an Outlook recipient of this email will see
From: me#domain.com sent on behalf of bingo#bongo.com
but the SendOnBehalf property in the Outlook mail item simply reads "me#domain.com".
Any advice much appreciated. Thanks.
Try setting SentOnBehalf like this
Option Explicit
Sub SetSentOnBehalf()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(0)
objMsg.SentOnBehalfOfName = "bingo#bongo.com"
objMsg.Display
MsgBox " SentOnBehalfOfName in the From: " & objMsg.SentOnBehalfOfName
Set objMsg = Nothing
End Sub