Always CC when sending "On Behalf Of" - vba

I often send emails on behalf of another user. I'd like to use VBA to automatically CC that user every time I send an email from/on behalf of that user.
I'm not familiar with VBA for Outlook but I'm thinking you could write an if statement that says "if sending message from UserX, cc UserX". The code should run automatically any time an email is sent on behalf.

SentOnBehalfOfName is tricky. It is usually empty until the item has been sent.
With this code in ThisOutlookSession you should find it blank.
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim myRecipient As Recipient
Debug.Print " item.SentOnBehalfOfName - " & item.SentOnBehalfOfName
If item.SentOnBehalfOfName = "someone#somewhere.com" Then
Set myRecipient = item.Recipients.Add("Someone Else")
myRecipient.Type = olCC
item.Recipients.ResolveAll
End If
End Sub
At least one way to get around this:
Sub createSentOnBehalf()
Dim objMsg As mailitem
Set objMsg = Application.CreateItem(olMailItem)
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
objMsg.Display
Set objMsg = Nothing
End Sub
Sub replySentOnBehalf()
Dim objMsg As mailitem
Set objMsg = ActiveInspector.currentItem.reply
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
objMsg.Display
Set objMsg = Nothing
End Sub
Edit: Just realized you could set the cc while creating / replying rather than waiting until ItemSend.
Edit2: Move the cc code from itemsend
Sub createSentOnBehalf()
Dim objMsg As mailitem
Dim myRecipient As Recipient
Set objMsg = Application.CreateItem(olMailItem)
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
Set myRecipient = objMsg.Recipients.Add("Someone Else")
myRecipient.Type = olCC
objMsg.Recipients.ResolveAll
objMsg.Display
Set objMsg = Nothing
End Sub

This will do what you are looking for (It is the first Google results of "always CC myself Outlook")
http://www.extendoffice.com/documents/outlook/1108-outlook-auto-cc.html
Launch your outlook 2013 or 2010, and make sure that you are in the mail section. Then click Home > Rules > Manage Rules & Alerts.
After selecting Manage Rules & Alerts option, the Rules and Alerts dialog will popup. Under E-mail Rules, click New Rule option.
In the Rules Wizard, click Apply rule on messages I send then click Next to continue.
Then another dialog pops up.
(1.) In Step 1, check through the specified account box. In Step 2, please click on the word - specified.
(2.) And then click the Account drop down list to choose the account that you want to apply this rule.
After selecting the account, and click OK to return to the previous window, you will see the selected account showing in the Rules Wizard. Then click on Next button.
(1.) In this wizard, check Cc the message to people or public group box, and then click on people or public group in step 2.
(2.) In the Rule Address dialog box, double click your cc recipient to add the address to the To-> text box, (If I want to cc myself, I will select or type my own email address in the To-> column.), finally click OK.
It returns to the previous window, and you can see the cc recipient address appearing. Then click Finish button.
Now, it returns to the very beginning dialog, click OK button, then the cc rule will be created. If you don’t want to enable the rule, uncheck it.
Then after sending or forwarding an email message to others with your specified account, your account or your specific cc recipient will always receive the same message.

It looks like you need to handle the ItemSend event of the Application class. It is fired whenever 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. Note, the Cancel parameter allows to cancel the process of sending the email.
In the ItemSend event handler you can check out the SentOnBehalfOfName property of the item passed as a parameter and add the CC recipient using the Recipients property of the MailItem class. The Recipients collection provides the Add method for adding recipients.
Set myRecipient = myItem.Recipients.Add("Dan Wilson")
myRecipient.Type = OlMailRecipientType.olCC
After don't forget to call the Resolve or ResolveAll method of the Recipient class to resolve a Recipient object against the Address Book.
See How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.

Related

Forward an email received in shared mailbox, setting FROM to a specific user account

I receive invoices from suppliers that I want to forward to a QBO (Quickbooks Online) email address that handles 'receipts/invoices' wherein it reads the attachments and parses the info within - this speeds up data entry.
QBO only accepts these emails from specific email addresses (i.e., ones that are registered as accounts in QBO). So assume that QBO only accepts emails from "me#mydomain.com". I receive the invoices on "billing#mydomain.com" which is a shared Office 365 mailbox that "me#mydomain.com" has access to.
My VBA code should forward the currently selected emails (found within the billing#mydomain.com mailbox) using the me#mydomain.com sending address to mydomain#qbodocs.com.
The problem is that the forwarded email arrives in the recipient's mailbox as having come from billing#mydomain.com. When I .Display (instead of .Send), I see that the sending account is set "correctly" yet it still arrives from the wrong account.
I decided that after the window pops up (using .Display), I would change the sending account to something else, then back to the intended sending account - and it works. So there's some setting/headers other than .SentOnBehalfOfName that I need to set as I don't want any user intervention.
Option Explicit
Public Sub SendToQBO()
Dim Email As Object
Dim Sender As String
Sender = "me#mydomain.com"
For Each Email In ActiveExplorer.Selection
With Email.Forward
' Just send to myself for now until this is figured out
.To = Sender
'.To = "mydomain#qbodocs.com"
.Subject = "Sent From Outlook"
.Body = Email.Body
.SendUsingAccount = Session.Accounts(Sender)
.SentOnBehalfOfName = Sender
.Send
' Using .Display instead shows the right sending address, but it's ineffective
' unless I select another, then select it again before manually sending.
' .Display
End With
Next
End Sub
There is no need to set up two properties simultaneously on the mail item.
The MailItem.SendUsingAccount returns or sets an Account object that represents the account under which the MailItem is to be sent. The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called. Note, corresponding account should be configured in Outlook:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
In case given permissions to the shared account you can use the MailItem.SentOnBehalfOfName property which allows setting a string indicating the display name for the intended sender of the mail message. In that case the account may not be configured in Outlook.
And the last bit which I've noticed in your code:
For Each Email In ActiveExplorer.Selection
Remember, the selection object may contain different kind of Outlook items - appointments, posts, mail items and etc. So, I'd suggest adding a check for the item type before accessing MailItem methods and properties.

Only run a macro if there is an email open

I have a script to process emails. User's can kick this script off by using a form.
I want them to only be able to use the form if they have an email open and in focus. So how can I check that the CurrentItem in:
objApp.ActiveInspector.CurrentItem
Is an email and is not another open window?
To work with mail item that is open and has focus, use ActiveInspector method
Example blew to print subject if Item is Mailitem
Option Explicit
Sub Item_Info()
Dim Active_Item As Object
Set Active_Item = Application.ActiveInspector.CurrentItem
If TypeOf Active_Item Is Outlook.MailItem Then
Debug.Print Active_Item.Subject
End If
End Sub

Change account settings in Outlook 2010 using VBA

I have a large number of forwarding email addresses which are all set to forward to the same email account. I find this is useful because if a business is hacked and my email address is stolen then I only have the change the email address for that business. For example, "amazon#mydomain.com", "ebay#mydomain.com" and "facebook#mydomain.com" would all be forwarded to "mailbox#mydomain.com".
When I want to send an email to the business, I have to go into Outlook and change the account set up to have the forwarding email address as the email address. I find this a nuisance. I know I can change who the email is from when I write it, but then the recipient sees "J Smith on behalf of newaddress#mydomain.com". I would rather it just showed the address I am using in the from field, as it does if I go into the account set up and change the email address there.
It would be nice to have a macro set up which asked me which email address I wanted to use and then sent the email for me. I have looked up how to change email account details in VBA, but it looks as if the details are all read-only. Is there a way to change my "from" email address cleanly? Or even setting up a new email account in VBA and deleting it immediately after sending it?
Try creating a userform with a combobox and a button on it. Load all your available accounts into the combobox to be able to select from it:
Private Sub UserForm_Initialize()
Dim acc As Account
For Each acc In ThisOutlookSession.Session.Accounts
Me.ComboBox1.AddItem acc.UserName
Next acc
End Sub
Then add some code to the button that selects the proper account:
Dim objApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set objApp = ThisOutlookSession.Application
Set objMail = objApp.CreateItem(olMailItem)
With objMail
.To = "lala#lala.com"
.CC = ""
.BCC = ""
.Subject = "Test"
.Body = "Test"
Dim i As Integer
For i = 1 To ThisOutlookSession.Session.Accounts.Count Step 1
If ThisOutlookSession.Session.Accounts.Item(i).UserName = Me.ComboBox1.Value Then
.SendUsingAccount = ThisOutlookSession.Session.Accounts.Item(i)
End If
Next i
.Display
End With
Maybe there is an event that is called when you are creating a new email, otherwise you have to add a button or something to bring the form up.
I had this exact same problem and ended up being able to solve it by installing Outlook Redemption and using the following script...
' Redemption code below. Must install Redemption to work.
' http://www.dimastr.com/redemption/faq.htm#14
Dim sItem, Tag
Set sItem = CreateObject("Redemption.SafeMailItem")
sItem.Item = oMailItem
Tag = sItem.GetIDsFromNames("{00020386-0000-0000-C000-000000000046}", "From")
Tag = Tag Or &H1E 'the type is PT_STRING8
sItem.Fields(Tag) = GetHashedReply(oMailItem)
Tag = sItem.GetIDsFromNames("{00020386-0000-0000-C000-000000000046}", "Sender")
Tag = Tag Or &H1E 'the type is PT_STRING8
sItem.Fields(Tag) = GetHashedReply(oMailItem)
sItem.Subject = sItem.Subject 'to trick Outlook into thinking that something has changed
sItem.Save
...where oMailItem is a normal Outlook MailItem that you can get with CreateItem() or get passed to you in the ItemSend() parameters.

Outlook - Forward selected items as attachments macro - issue with forward symbol

I found the below code from a very helpful post by user thommck. It forwards selected items as attachments in separate emails to a specified recipient.
When I use the code, the forward symbol does not appear on the email icon of the email I just forwarded. If I use the regular Outlook method for "Forward as Attachment," the symbol is added to the envelope icon in my viewing pane.
Any ideas on how to get this forward symbol to appear when using this code?
Sub ForwardSelectedItems()
On Error Resume Next
Dim objItem As Outlook.MailItem
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.Attachments.Add objItem, olEmbeddeditem
.Subject = "enter text"
.To = "example#example.com"
.Send
End With
Next
Set objItem = Nothing
Set objMsg = Nothing
End Sub
You need to use the Forward method of the MailItem class instead of creating a new mail item in the code:
Application.CreateItem(olMailItem)
should be replaced with:
objItem.Foward()
Note, you need to clear the message body if you don't want to see the content of the attached item.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
You can simulate pressing a button with ExecuteMso.
https://msdn.microsoft.com/en-us/library/office/ff862419%28v=office.15%29.aspx
expression.ExecuteMso(idMso)
expression An expression that returns a CommandBars object.
To find the idMso, go through the process of adding a button to the Quick Access Toolbar or a ribbon. Once you find the control in the list, hover over it until the tooltip appears. The idMso is the name in brackets.
In this case it is ForwardAsAttachment

how to clear outbox with vba in outlook, still got one outgoing email

I want to "send" a message to specific recipient but do not want to deliver. The message is in the outbox but I want to delete it immediately. When I use the command delete it says it was deleted before sending. I do not want popups and also there is an alert when exiting Outlook that there are still unsent messages. How to clear outbox?
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim outApp As Outlook.Application
Dim deletedFolder As Outlook.MAPIFolder
Dim entryID As String
Set outApp = CreateObject("outlook.application")
Set deletedFolder =outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox)
If item.To = "example#mail.com" Then
item.DeferredDeliveryTime = DateAdd("y", 99999, Now)
End If
Set deletedFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox)
If deletedFolder.Items.Count >= 0 Then
For i = 1 To deletedFolder.Items.Count
deletedFolder.Items(1).Delete
Next i
End If
End Sub
The Outbook folder contains yet unsent messages. It is definitely not the right time and place for deleting items because they were not sent. Instead, I'd suggest handling the ItemSend event and adding a user property (i.e. marker which can tell you later whether you need to delete that message or not), see UserProperties property of the MailItem class for more information. For example:
Set myUserProperty = myItem.UserProperties _
.Add("YourDetails", olText)
myUserProperty.Value = "Delete"
Then in the ItemAdd event handler of the Sent Items folder you can check out the user defined property value and delete the sent message if required. Be aware, the Sent Items folder is set as a target folder by default for sent items. But you can set a custom folder programmatically, see the SaveSentMessageFolder property - a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.