How to send mail from shared email account? - vba

I am trying send several items at the same time.
I have two mail addresses in my Outlook. The first is a personal work mail (like j.doe#company.com) and second is a shared mail account (like support#company.com).
I tried:
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
Dim oAccount As Outlook.account
Set oAccount = Session.accounts.Item(2)
objMailMessage.SendUsingAccount = oAccount
objMailMessage.display
I found this won't work with shared email accounts.
Most forums advise to use .SentOnBehalfOfName. When I try it on one email, the email has something like this in the Sender box j.doe#company.com - Sent on behalf of name: "support#company.com.
When I send mail from Outlook manually, it only shows the shared account on the received message. (It is not a big deal, but would be nicer if the code would work in the same style as manually.)
Also, when I put the code in a loop, half mails are sent with shared accounts and half with personal account.
And here is the code with .SentOnBehalfOfName which is going to be looped.
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
With objMailMessage
.To = email
.Subject = msgSubj
.CC = ccp
.BCC = "support#company.com"
.SentOnBehalfOfName = "support#company.com"
.HTMLBody = msgText & "<br>" & "<br>" & msgSign
.Attachments.Add path
If rev > 0 Then
.Save
Else
.Send
End If
End With

This code attempts to ensure the .SentOnBehalfOf is consistent.
Option Explicit
Sub sendFromOtherMailbox()
' Code in Outlook, not called from another application
Dim objMailMessage As mailItem
Dim uMailbox As recipient
' Should not be necessary but this is used later to ensure
' the entry in .SentOnBehalfOfName is what you think it is.
Set uMailbox = Session.CreateRecipient("Preferably display name here rather than email address.")
uMailbox.Resolve
' An email address always resolves so the "If Resolved" test is not useful
' if an email address was used in .CreateRecipient
If uMailbox.Resolved Then
Set objMailMessage = CreateItem(olMailItem)
With objMailMessage
.Subject = "send From Other Mailbox"
.SentOnBehalfOfName = uMailbox
.Display
End With
End If
End Sub

Related

.SentOnBehalfOf and .SendUsingAccount on forward

I go into a shared inbox and forward an email as myself, to my boss, using HTML formatting.
I wrote VBA code that works on every step except changing the From line from the shared mailbox email address to my email address.
Let's call my personal email address "gwyn#email.com" and the shared mailbox email address "office#email.com". If I manually click on the From field I can select my own email address and send it.
Public Sub ForwardIAF()
Dim OutApp As Object
Dim OutAccount As Outlook.Account
Dim myinspector As Outlook.Inspector
Dim myIAF As Outlook.MailItem
Dim myForward As Outlook.MailItem
Set myIAF = GetCurrentItem()
Set myForward = myIAF.Forward
Set OutApp = CreateObject("Outlook.Application")
Set OutAccount = OutApp.Session.Accounts.Item(1)
With myForward
.SentOnBehalfOfName = "gwyn#email.com"
Debug.Print "myForward.SentOnBehalfOfName:" & "x " & myForward.SentOnBehalfOfName & " x"
.Recipients.Add "gwyn#email.com"
.BodyFormat = olFormatHTML
.Display
End With
End Sub
When the forward opens, it shows my email address in the From line, but when I send, it reverts to the office email address. The debug print shows my email address is in the .SentOnBehalfOf field, so it looks like it's there until it sends.
Replacing .Display with .Send has the same result.
The SentOnBehalfOfName property makes sense only in case of Exchange profiles/accounts. Moreover, you need to have the required permissions to send on behalf of another person. See Issue with SentOnBehalfOfName for a similar discussion.
In case if you have multiple accounts configured in the profile you can use the SendUsingAccount property which allows to an Account object that represents the account under which the MailItem is to be sent.
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
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub

Outlook vba - How can i add suffix on mail subject before forward, reply or replay to all?

I need to forward or reply to a mail, then add suffix on subject, and send it.
thanks in advance
You could reply or replyAll an email and add suffix to subject using the below code:
Option Explicit
Sub ReplyMSG()
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply
Dim olRecip As Recipient ' Add Recipient
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
olReplay.Subject = = olItem.Subject & “suffix”
Set olRecip = olReply.Recipients.Add("Email Address Here") ' Recipient Address
olRecip.Type = olCC
olReply.HTMLBody = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
olReply.Display
'olReply.Send
Next olItem
End Sub
Reference from:
Outlook Reply or ReplyAll to an Email
If you want to forward an email and add suffix to subject, please refer to the below code:
Sub ForwardEmail(item As Outlook.MailItem)
Dim oMail As MailItem
On Error GoTo Release
If item.Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject & “suffix”
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"
oMail.Save
oMail.Send
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub
Reference from:
Forward Email with its attachment in Outlook 2010
Properly going to be hard, because depending on your hierarchy location you would only have a set number of rights and i do not think VB Macro would be allowed i know in many organizations here in Denmark VB Macros is a no no.
But you can create a Macro in the background where you grep Current mail and then alter the subject and then set it to when you press CTRL + R as you normally do when you reply but it might conflict so be careful because you might not want it on all emails. that you reply to.

VBA outlook - send email and categorize the CC email

I have been using public folder and sending emails on outlook. Whenever we send emails with the public folder email address (e.g info#company.com), we will CC info#company.com. So that we will keep the email in our public folder.
I have created some VBA code to send outlook email automatically.
My question is: Is it possible to automatically categorize (e.g. customer-inquiry) the email in our public folder when I send the email without showing the category to recipient (as .
Sub outlookActivate1()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
With OutMail
.BCC = ""
.BodyFormat = olFormatHTML
.Categories = "customer-inquiry"
.ShowCategoriesDialog
.Save
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set oItem = Nothing
End Sub
No, it is not. You may consider adding a user property to the mail item instead. See UserProperties for more information.

Send an email from a group email address in outlook using VBA

I currently want to build a VBA function that enables people to send emails using a group email address(e.g. person A has an email address a#111.com and he is also a member of "student" group and has access to send emails using the groups email address student#111.com)
I am thinking about using a VBA to build such a function. It is easy to construct body, recipient and etc. but how to shift the sender i.e. from field to the group email address?
Did you want any more than just how to send it? I'm slightly confused by your question.
Sub Mail_Workbook_1()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run it. Or pass variables to it
With OutMail
.To = "tom#google.com" 'You can also set it equal to something like TextBox1.Text or any string variable or item
.CC = ""
.BCC = ""
'Once again for the next two you can pull this from a cell, a textbox, or really anything
.Subject = "This is the Subject line"
.Body = "Hello World!"
.Attachments.Add ActiveWorkbook.FullName
' You can add other files by uncommenting the following line.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Maybe you just need to edit the reply-to address so that any replies get sent to the group?
Here's how, using Outlook:
'Tools > References ... > check "Microsoft Outlook object library"
Dim outlookApp As Outlook.Application
Dim mailMsg As MailItem
Dim replyToRecipient As Recipient
Set outlookApp = CreateObject("Outlook.Application")
Set mailMsg = outlookApp.CreateItem(olMailItem)
With mailMsg
.To = "abc#111.com"
Set replyToRecipient = .ReplyRecipients.Add("group#111.com") ' group adderss
replyToRecipient.Resolve
If Not replyToRecipient.Resolved Then Err.Raise 9999, , _
replyToRecipient.Address _
& " could not be resolved as a valid e-mail address."
'...
'... edit body etc. here...
'...
.Display
End With

VBA Outlook Mail .display, recording when/if sent manually

My code displays a message with basic subject, body, attachment. Next the user manually updates and customizes the message and should send it. I want to record when (if) the email is sent. Is this possible or any tips?
My environment is Office 2007 with an excel based macro going to Outlook.
[Excerpt]
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = Email '.CC =
.Subject = Subj
.BodyFormat = olFormatHTML
.Body = Msg '.HTMLBody = Msg
If Not FileAttach = vbNullString Then .Attachments.Add (FileAttach)
.Display
End With
This is entirely possible, using the _Send event in the Outlook.MailItem class.
The way I use it, I create a class called EMail Watcher, so when I create the email and do the .Display, I then create a new EMailWatcher object and tell it to watch that email for send, then report back when it happens.
Here's the class as I use it. Basically, I also optionally can set the BoolRange so that if the user sends the email, that Excel range gets updated with True. I can also have the class update an Excel range with the time the email is sent.
Public BoolRange As Range
Public DateRange As Range
Public WithEvents TheMail As Outlook.MailItem
Private Sub TheMail_Send(Cancel As Boolean)
If Not BoolRange Is Nothing Then
BoolRange.Value = True
End If
If Not DateRange Is Nothing Then
DateRange.Value = Now()
End If
End Sub
And here's how I use it:
With oMail
.To = addr
.Subject = "CCAT eVSM Utilities License Code"
.Body = "Message body"
.Display
End With
Set CurrWatcher = New EmailWatcher
Set CurrWatcher.BoolRange = Range("G12")
Set CurrWatcher.TheMail = oMail
Hopefully that helps...