How to update a Sender EmailAddress - vba

I am sending email on behalf of a Shared MailBox - generic system account?
How do I update the sender in Outlook mail?
I am getting a Run-Time error '438': Object doesn't support this property or method .From = "MYACCOUNT#ACCOUNT.com"
Function CreateEmail(MySQL As String)
'On Error GoTo Exit_Function:
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem 'rs As Recordset
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(MySQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
Do Until rs.EOF
If IsNull(rs!standard_e_mail_addr) Then
rs.MoveNext
Else
If oOutlook Is Nothing Then
Set oOutlook = New Outlook.Application
End If
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
.To = rs!standard_e_mail_addr
.From = "MYACCOUNT#ACCOUNT.com" ' **
.Subject = "Mandatory Action Required Submit In-Person Identification Form for " & rs!emp_fname
.Body = "EmpNo: " & rs!emp_no & vbCr & _
"EmpName: " & rs!emp_fname & vbCr & _
"DO NOT REPLY."
.Display
.Send
rs.Edit
rs!EmailNotification_Send = Date
rs.Update
End With
Set oEmailItem = Nothing
Set oOutlook = Nothing
rs.MoveNext
End If
Loop
Else
End If
rs.Close
Exit Function:
Exit Function
End Function

Okay, try: .SentOnBehalfOfName = """SenderName"" <MyAccount#Address.com>"
Also review: Use another account for sender

First of all, there is no need to call Display before calling the Send method.
If you have the shared mailbox configured in Outlook you need to use the SendUsingAccount property which allows to set an Account object that represents the account under which the MailItem is to be sent. For example:
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
Use SentOnBehalfOfName as long as your Exchange account has SendAs permission for the shared mailbox or distribution group, it will be sent from the shared account or group, not sent on behalf of.
With oEmailItem
.To = rs!standard_e_mail_addr
.SentOnBehalfOfName = "MYACCOUNT#ACCOUNT.com"
.Subject = "Mandatory Action Required Submit In-Person Identification Form for " & rs!emp_fname
.Body = "EmpNo: " & rs!emp_no & vbCr & _
"EmpName: " & rs!emp_fname & vbCr & _
"DO NOT REPLY."
.Send

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.

Loop through list of email addresses in recordset to send tailored emails

I want to loop through a table and to email each user an individually tailored email with their prefix and last name.
It seems to be only emailing the first person on the list.
Design mode
Form mode with dummy data
Private Sub SendEmail_Click()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
Dim rs As DAO.Recordset
On Error Resume Next
Err.Clear
Set oOutlook = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set oOutlook = New Outlook.Application
End If
Set oEmailItem = oOutlook.CreateItem(olMailItem)
Set rs = CurrentDb.OpenRecordset("SELECT * FROM list_of_emails")
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Do Until rs.EOF = True
With oEmailItem
.To = rs!Email
.Subject = "NKS: Test"
.Body = "Hi " & [Prefix] & " " & [lname] & ":" & vbCrLf & vbCrLf & "This is a test."
.Send
End With
rs.MoveNext
Loop
End If
rs.Close
Set oEmailItem = Nothing
Set oOutlook = Nothing
Set rs = Nothing
End Sub
If I remove the On Error Resume Next, I get the following error when I assign the recipient address (.To = rs!Email):
The item has been moved or deleted.
as the comments indicate you just have a bunch of errors. assuming you have a reference to outlook 16 object library added and Prefix and lname are columns in the list_of_emails table then:
Private Sub SendEmail_Click()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
Dim rs As DAO.Recordset
'On Error Resume Next
'Err.Clear
'Set oOutlook = GetObject(, "Outlook.Application")
'If Err.Number <> 0 Then
' Set oOutlook = New Outlook.Application
'End If
Set oOutlook = New Outlook.Application 'open outlook before start the loop
Set rs = CurrentDb.OpenRecordset("SELECT * FROM list_of_emails")
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Do Until rs.EOF = True
Set oEmailItem = oOutlook.CreateItem(olMailItem) 'create new email for each email address
With oEmailItem
.To = rs!Email
.Subject = "NKS: Test"
.Body = "Hi " & rs!Prefix & " " & rs!lname & ":" & vbCrLf & vbCrLf & "This is a test."
.Send
End With
rs.MoveNext
Loop
End If
rs.Close
Set oEmailItem = Nothing
Set oOutlook = Nothing
Set rs = Nothing
End Sub

How to reference item that triggers ItemAdd?

I'm trying to send a message to my phone when I receive mail at work.
The macro should send mail to an sms service that converts the mail to an sms and sends it to my phone. The message will contain the mail sender address and the send and receive times.
I have put together two macros I found by searching the internet.
The first code is on this link tachytelic.net
The second I found here
stackoverflow.com
Here is the part of the code that fails.
'variable for select case
Dim EmailFrom As String
Dim OldMessage As Outlook.MailItem
Set OldMessage = Application.ActiveInspector.CurrentItem
'Puts sender mail address in variable both ordinary mail and Exchange emails.
Select Case OldMessage.SenderEmailType
Case "EX"
EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
Case Else
EmailFrom = OldMessage.SenderEmailAddress
End Select
'Sends E-mail to sms service.
If TypeName(Item) = "MailItem" Then
With olEmail
.BodyFormat = olFormatPlain
.To = "some#mail.com"
.Subject = "You got a new E-mail!"
.Body = EmailFrom & vbCrLf & "Sendt: " & Item.SentOn & vbCrLf & "Modtaget: " & Item.ReceivedTime
.Send
End With
End If
I get
runtime error 91 - Object variable or With block variable not set.
I tried to use the F8 key but that isn't possible, I don't know why.
Then I took the original code and pasted it in a module. Then I can use the F8 key to go through the code.
The error comes when I reach this line.
Set OldMessage = Application.ActiveInspector.CurrentItem
Here is the whole code
Option Explicit
Private WithEvents inboxItems As Outlook.Items
Sub Application_Startup()
Dim outlookApp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookApp = Outlook.Application
Set objectNS = outlookApp.GetNamespace("MAPI")
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
'variable for if statments
Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)
'variable for select case
Dim EmailFrom As String
Dim OldMessage As Outlook.MailItem
'Deletes sms status mails I recieve when I mail to sms service
If InStr(Item.Subject, "SMS status") > 0 Then
Item.UnRead = False
Item.Save
Item.Delete
End
End If
'Puts sender mail address in variable both ordinary mail and Exchange emails.
Select Case OldMessage.SenderEmailType
Case "EX"
EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
Case Else
EmailFrom = OldMessage.SenderEmailAddress
End Select
'Sends E-mail to sms service.
If TypeName(Item) = "MailItem" Then
With olEmail
.BodyFormat = olFormatPlain
.To = "some#mail.com"
.Subject = "You got a new E-mail!"
.Body = EmailFrom & vbCrLf & "Sendt: " & Item.SentOn & vbCrLf & "Modtaget: " & Item.ReceivedTime
.Send
End With
End If
ExitNewItem:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ExitNewItem
End Sub
Then it is easier to get an overview.
How do I get the ActiveInspector to se the recieved mail and save it in the OldMessage?
If there is no open item window of any kind, ActiveInspector will be Nothing. Also, based on your supplied code sample, the Item variable is not declared or set anywhere, so you'll likely also get an error on this line:
If TypeName(Item) = "MailItem" Then

Add a "CC recipient" to Outlook 2010 VBA

Can someone please show me how to add a "Cc recipient" to this code? The "To Recipient" and code all work as intended. Thank you for your time.
Sub ForwardEmail(item As Outlook.MailItem)
' Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
' Set oExplorer = Application.ActiveExplorer
On Error GoTo Release
' If oExplorer.Selection.item(1).Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject
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
set oRecip = oMail.Recipients.Add("email address here")
oRecip.Type = olCC
or
oMail.CC = "email address here"

Forward Email with its attachment in Outlook 2010

The below code (I pulled from several sources) now works in that when I receive an email with specific words in the subject line it triggers a script that runs the below.
This code then keeps the subject line, adds text the message body and the forwards to the intended recipient.
However, if the email I receive has an attachment the code no longer forwards anything. I need it to forward the attachment that was emailed to me as well (only using the code to add text to body of email otherwise I would just set a rule).
CODE BELOW:
Sub ForwardEmail(item As Outlook.MailItem)
Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer
On Error GoTo Release
If oExplorer.Selection.item(1).Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject
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
There is no need to use the Explorer object in the 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
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
You may find the Getting Started with VBA in Outlook 2010 article helpful.
There is an unnecessary condition
If oExplorer.Selection.item(1).Class = olMail Then
that may cause the forwarding to be bypassed.
Sub ForwardEmail(item As Outlook.MailItem)
' Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
' Set oExplorer = Application.ActiveExplorer
On Error GoTo Release
' If oExplorer.Selection.item(1).Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject
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