How to send from a shared non-Exchange account? - vba

How can I change the .sentonbehalfofname property?
We changed shared mailbox from Exchange to normal account. Mailbox added to Outlook as a normal account.
With the following code I get:
"This message could not be sent. You do not have the permission to send the message on behalf of the specified user."
' Construct email
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = "bbb#aaa.com"
.BodyFormat = 2
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.To = currentEmailAddress
.Subject = emailTitle & " ID:" & Range(teamMemberIDColumn & cell.Row).Value
.Display
.Send

Try this
'Construct email
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SendUsingAccount = Application.Session.Accounts("bbb#aaa.com")
.BodyFormat = 2
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.To = currentEmailAddress
.Subject = emailTitle & " ID:" & Range(teamMemberIDColumn & cell.Row).Value
.Display
.Send
Please note the account of bbb#aaa.com must be added and configured in your outlook for this to work.

According to the error message, you don't have permissions to send on behalf of another person. You must contact your Exchange administrator to grant such permissions.
Another possible way to send as another person is to use the MailItem.SendUsingAccount property which sets an Account object that represents the account under which the MailItem is to be sent. In that case, you need to configure the other account in the
Outlook profile. Here is the sample use case scenario:
OutMail.SendUsingAccount = Application.Session.Accounts(1)

Related

Sending email from other account

I send emails through Outlook using Excel VBA. I am using my work computer with my work email as the main account, but want to send from another account that is logged in.
I have not managed to integrate any of the code found online.
The below code is without my attempt of fixing it.
Sub Test1()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String
For Each cell In Range("D2:D2")
strbody = strbody & cell.Value & vbNewLine
Next
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Do you mean send from another email address? If so just add :
.SentOnBehalfOfName = "Email#Other.com" 'Change to the email address you want to send from
e.g
With OutMail
.To = cell.Value
.SentOnBehalfOfName = "Email#Other.com"
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
There are two possible ways in Outlook:
If another account is configured in Outlook you need to use the MailItem.SendUsingAccount property which returns or sets 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
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
If you have got permissions set by the Exchange admin to send on behalf of another person you need to use the MailItem.SentOnBehalfOfName property which returns a string indicating the display name for the intended sender of the mail message.

How to include sender's details in email body?

I have a macro in MS Word which:
Creates an Outlook email on the click of a button
Attaches the Word document to the Outlook email
I want to include the sender's details (name, email address, etc.) in the body of the email.
Dim Outlook_Object As Object
Dim Email_Object As Object
Dim This_document As Document
Application.ScreenUpdating = False
Set Outlook_Object = CreateObject("Outlook.Application")
Set Email_Object = Outlook_Object.CreateItem(olMailItem)
Set This_document = ActiveDocument
This_document.Save
With Email_Object
.Subject = "REPORT REQUEST FORM"
.Body = "This is a test email."
.To = "john.smith#gmail.com"
.Importance = olImportanceNormal
.Attachments.Add This_document.FullName
.Display
End With
Set This_document = Nothing
Set Email_Object = Nothing
Set Outlook_Object = Nothing
Application.ScreenUpdating = True
You can use the NameSpace.CurrentUser property which returns the display name of the currently logged-on user as a Recipient object. The Recipient.AddressEntry property returns the AddressEntry object corresponding to the resolved recipient. Then you can get the Name and Address properties like shown below:
Dim ns as Outlook.NameSpace
Set ns = OutlookApplication.GetNamespace("MAPI")
' to get the email address of the sender
ns.CurrentUser.AddressEntry.Address
' to get the name
ns.CurrentUser.AddressEntry.Name
To include this information to the message body you can use the Body or HTMLBody properties.
Ended up using WScript.Network
Function GetUserFullName() As String
Dim WSHnet, UserName, UserDomain, objUser
Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
GetUserFullName = objUser.FullName
End Function
Also, instead of using .body, I used .HTMLbody for the email message body [where I invoked GetUserFullName()]
.HTMLbody = "Request submitted by:" & "<br>" & GetUserFullName()

Using VBA to Save an email to Drafts or Display before sending in Gmail

Recently we moved from Outlook to Gmail at work and we are working on moving some of our VBA to work through Gmail. I was able to get all of my code to work if I want to send the email; however, I want to be able to see the emails before they are sent but am not sure if that is possible.
In outlook I was able to generate the email. Then add or change things before it was sent.
The .Display below does not work. .Send does work but again I want to be able to review these emails before they are sent. Is there anyway to do this in Gmail?
With iMsg
Set .Configuration = iConf
.To = Email_Send_To
.CC = Email_Cc
.BCC = Email_Bcc
.From = "Me"
.Subject = Email_Subject
.HTMLBody = email_body & "<br>" & "<br>" & .HTMLBody
.AddAttachment xlp
.Display
End With

Outlook - Auto-Forward Email with original attachment

I've seen this question posted several times but after looking extensively, I have not found a successful answer.
I'm trying to create a script for Outlook that auto-forwards an email with an attachment back to the sender, with an updated message, that includes the original attachment. It's all working EXCEPT no matter what I do I cannot forward the attachment. If I just use .Forward, the attachment is not included. If I try to use the .Attachment property, it breaks the script.
CODE:
Sub ForwardEmail(Item As Outlook.MailItem)
With Item.Forward
.Attachments.Add Item.Attachment
.Subject = ("APPROVED - ") & Item.Subject
.Recipients.Add Item.SenderEmailAddress
.Body = "Your timesheet has been approved." & vbCrLf & Item.Body
.Send
End With
End Sub
The Microsoft Outlook (and other email engines like Gmail, Yahoo) has been designed in this way:
When a mail is replied, the receiver do not need same attachments as the receiver already has them in their outbox (unless deleted). It saves storage space on server and network bandwidth too.
When a mail is forwarded, it is expected that forward is generally to at least a new user and hence all the content remain as is (i.e. attachments aren't removed)
Hence, please use this code. It follows the Rule#2 mentioned above i.e. whenever you forward an email, the attachment will be automatically attached.
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 = ("APPROVED - ") & Item.Subject
oMail.HTMLBody = "Your timesheet has been approved." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add Item.SenderEmailAddress
oMail.Save
oMail.Send
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub

How to send outlook email automatically using Excel UserForm?

Thanks for any help with this. I have a userform I've made that gathers criteria from the user and then when they hit submit it opens Outlook and emails that data to me.
I'm having 2 issues. The first is that when I try to use SENDKEYS method I'm running into the spell check feature stopping the email from actually sending without the user needing to go through it. Is there a way to bypass spell check and send the email?
Secondly, I couldn't find a way to actual send an email automatically without using SENDKEYS but I'm sure there is a better way out there to send the email rather than manipulating the window with TAB key strokes.
Private Sub SubmitButton_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strBody, RequestName, ProductName, Month, TestName, Summary As String
If Me.RequesterNameTxt.Value <> "" And Me.ProductCombo.Value <> "" And Me.MonthCombo.Value <> "" And Me.TestNameCombo <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
RequestName = Me.RequesterNameTxt.Value
ProductName = Me.ProductCombo.Value
Month = Me.MonthCombo.Value
TestName = Me.TestNameCombo.Value
Summary = Me.SummaryTxt.Value
strBody = "<HTML><BODY>"
strBody = "Requester Name: " & RequestName & "<BR>" & "Product Name: " & ProductName & "<BR>" & "Month: " & Month & "<BR>" & _
"Test Name: " & TestName & "<BR>" & "<BR>" & "Summary of Request: " & Summary
strBody = strBody & "</BODY></HTML>"
On Error Resume Next
With OutMail
.To = "example#gmail.com;"
.CC = ""
.bcc = ""
.Subject = "QA Service Request"
.htmlBody = strBody
.send 'This fixed my issue. I had this as .Display which opens email up and doesn't send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Application.SendKeys ("%s")
Else: MsgBox "Please fill out all form data before submitting request. Thank you!"
End If
End Sub
You need to use the Send method of Outlook items instead. The Send method sends an item using the default account specified for the session. In a session where multiple Microsoft Exchange accounts are defined in the profile, the first Exchange account added to the profile is the primary Exchange account, and is also the default account for the session. To specify a different account to send an item, set the SendUsingAccount property to the desired Account object and then call the Send method.
Also I'd recommend using the Recipients property for adding recipients instead. The property returns a Recipients collection that represents all the recipients for the Outlook item.