Accessing "From:" from VBA in Outlook 2010 - vba

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

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.

Reply, with template, using properties of original mail

I work at a Company that gets requests we filter in terms of
being eligibile for financing from our financing partner
requiring more information from the enquirer
There is a lot of monotonous work, in copy pasting email replies.
I looked into creating a Quick Action in Outlook, but because our mother company does not allow certain freedoms, like the Font has to be specifically Segoe Ui Light etc. I could not use this, so I thought about writing a macro.
I intended for a macro button to:
Open a new Reply email, replying to all in the original mail.
In this new email text body, use a template email that I already made and saved as a template. (This way it saved the Font, size and other formatting.)
Put in the original Sender and CC'ed mail addresses as well as the Subject from the original mail.
And then display the email, so I could make edits if I wanted to before sending.
Sub ReplyGewerbeanmeldung()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate(" "C:\Users\XYZ\AppData\Roaming\Microsoft\Templates\ReplyGewerbeanmeldung.oft"\ReplyGewerbeanmeldung.oft")
replyEmail.To = origEmail.Sender
replyEmail.CC = origEmail.CC
replyEmail.Subject = origEmail.Subject
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
replyEmail.Display
End Sub
First of all, it seems you need to correct the file path to the template message. The following string is not a valid file path:
" "C:\Users\XYZ\AppData\Roaming\Microsoft\Templates\ReplyGewerbeanmeldung.oft"\ReplyGewerbeanmeldung.oft"
Try to use the following string instead:
Set replyEmail = Application.CreateItemFromTemplate("C:\Users\XYZ\AppData\Roaming\Microsoft\Templates\ReplyGewerbeanmeldung.oft")
You can read more about that in the How To: Create a new Outlook message based on a template article.
Anyway, creating items from a template will not preserve the original message body. Moreover, in Outlook you will not get any visual appearance that a particular items was replied. So, you need to call the ReplyAll method on the original item in Outlook to avoid this artefacts in Outlook.
You can create a new mail item, but only for copying the message body response which is required to paste into the reply. The NameSpace.OpenSharedItem method can be used to to open iCalendar appointment (.ics) files, vCard (.vcf) files, and Outlook message (.msg) files. You may consider using it instead of creating a new item based on the template to grab the message body from there.

Outlook: count sent based on signature

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.

Turn on envelope icon for new mail for specific mailbox account only

I have two mailbox accounts in Outlook 2010.
Primary: Bob#something.com
Secondary: help#something.com
Every time I receive new mail to help#something.com I get the envelope of new mail.
I want to receive the envelope only for Bob#something.com
I cancelled the option to receive envelopes in Outlook.
I created a rule in Outlook 2010 under the account of Bob#something.com
I have the option to run a script but its empty.
I need to write VBA code that can do it:
If (mail was send to Bob#something.com) Then
show Envelope
End If
I know that I can just add the folder of help#something.com instead of its account but it is not possible in my environment (cloud users).
I searched on the web and didn't find anything like this. For example, I did not find what code line can turn the envelope on in Windows.
If you find displaying a message box is an acceptable alternative then the Run a Script format is described here Outlook's Rules and Alerts: Run a Script
From the rule you pass the new mail as "Item" like this:
Public Sub DisplayMessageBox(Item As Outlook.MailItem)
' You need not do anything with "Item" just use it as a trigger
MsgBox "New mail in Bob#something.com"
' or you can enhance the message using "Item" properties
MsgBox "New mail in Bob#something.com - Subject: " & Item.Subject
End Sub
You may also consider ItemAdd or NewMailEx which are a little more complex.
There is an ItemAdd example here How do I trigger a macro to run after a new mail is received in Outlook?

Outlook auto forward set replyto to orginal sender rather than forwarder

I have VBA code to forward email to a specific account. It works except email being forwarded has the forwarder's email address.
How can I keep the original sender email address as the replyto after an email is forwarded?
Sub AutoForwardAllSentItems(Item As Outlook.MailItem)
Dim strMsg As String
Dim autoFwd As Outlook.MailItem
Set autoFwd = Item.Forward
autoFwd.Recipients.Add "my_email#domain.com"
autoFwd.Send
Set autoFwd = Nothing
End Sub
so there is no way? really? – Mike 7 hours ago
Riking is correct when he mentioned that Outlook will not let you modify the headers included in the email. I am guessing that he is refering to .SenderEmailAddress property. You cannot modify the .SenderEmailAddressas this property is readonly.
Having said that there is another property that you may like to use. .SentOnBehalfOfName More details here
Topic: SentOnBehalfOfName Property
Link: http://msdn.microsoft.com/en-us/library/aa171998%28v=office.11%29.aspx
Quote from the above link
Returns a String indicating the display name for the intended sender of the mail message. This property corresponds to the MAPI property PR_SENT_REPRESENTING_NAME. Read/write.
expression.SentOnBehalfOfName
expression Required. An expression that returns a MailItem object.
Also see this link
Topic: Automatically setting the ‘From’ address of a new Outlook message
Link: http://benchristian.wordpress.com/2005/12/18/automatically-setting-the-from-address-of-a-new-outlook-message/
Quote from the above link
Setting an alternate reply address is particularly useful if you are using a mail enabled public folder or distribution list for a group of users and would like the replies to messages that they send to go to the group smtp address instead of the sender’s mailbox.
HTH
Everything I've seen so far supports the conclusion that Outlook will not
let you modify the headers included in the email.
Sorry. I'd suggest managing the forwards at the email provider, if that is an option for you.