Problems with changing sender field and account in emails received from specific account - vba

I have below code which changes sender field and also changes account from which the email is being sent.
My account is authorized to send also from this email.
Code works well on new emails and also works well replied emails, but does not work well on one of the accounts I have. I have 3 accounts (POP, #outlook.com and Exchange). If email has been previously received from an #outlook.com account (this account is also set as exchange account) or set to be sent from an #outlook.com account it does not do the job. For example: If I open new email, set sending account to XX#outlook.com and then run code it does not work. Similar if I reply to an email, which I have received from xx#outlook.com account the code does not work.
Code works well if my email (replied or new) is previously set to be sent from my POP account or from my third-exchange account. The code does not work, if my email (replied or new) is previously set to be sent from the #outlook.com account.
Why is the code not working on all accounts?
Sub ChangeSender()
Dim NewMail As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveInspector
If oInspector Is Nothing Then
MsgBox "No active inspector"
Else
Set NewMail = oInspector.CurrentItem
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
NewMail.SentOnBehalfOfName = "test#test.com"
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
End If
Next
NewMail.Display
End If
End If
End Sub
In case previously set account is the POP one or the third-exchange account the code works well.
In case previously set account is the xx#outlook.com account, the code does not work.
In VBA I can see the whole code runs through, but there is no change on the line >>NewMail.SendUsingAccount = oAccount as is in case when working properly.
This is my first VBA code, so maybe I am missing some basics.
EDIT:
Here is a visual image of what I am trying to do with VBA. Same think as I do with several mouse clicks and some typing: Change the email FROM field and change the account from which I am sending email from.
Image
EDIT2:
Now I have tried to remove line .SentOnBehalfOfName altogether and the script again works only on POP account and does not work on outlook.com Exchange account. In other words: I left only the line .SendUsingAccount and if email is preset to POP account it changes account correctly, if email is preset to outlook.com exchange account it does not change to the Exchange accounttest#accounttest.com account.
Any suggestion why some accounts can be changed to another one, some cannot be changed to another one?
EDIT3:
Above is image how I am sending email by hand (mouse/keyboard) with from field to test#test.com and it is being sent through my account accounttest#accounttest.com. I am doing this successfully for several months.
For the last 2 weeks I have been using partially successfully above VBA code to achieve the same. VBA code works great if before running the code my email from field is set to my POP account or my Exchange account (accounttest#accounttest.com). Above VBA code does not work in case if before running it, my email from field is set to XX#outlook.com account.
There are obviously some restrictions which account can change to which with VBA. When changing by hand, there are no restrictions.
I think SentOnBehalfOfName line is not a question any more (it is working great when I change account successfully). Maybe I should make a new question altogether without this line?

You need to set either SentOnBehalfOfName or SendUsingAccount property, but not both unless you set an SendUsingAccount to an Exchange account and SentOnBehalfOfName to a GAL user name from that Exchange account.

In the following code:
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
NewMail.SentOnBehalfOfName = "test#test.com"
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
End If
Next
NewMail.Display
End If
The SentOnBehalfOfName is set without making sure you deal with an Exchange account set for the MailItem.SendUsingAccount property. It seems the chosen account in Outlook (or the default one) doesn't have permissions for sending on behalf of another person. Just need to keep the order of setting properties.
So, let's assume the accounttest#accounttest.com account belongs to Exchange where you have sufficient permissions for sending on behalf of another person. The code should look like that then:
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
' when you deal with an Exchange account
NewMail.SentOnBehalfOfName = "test#test.com"
End If
Next
NewMail.Display
End If
You can't set the SentOnBehalfOfName for every account in Outlook, it does make sense only for Exchange with sufficient permissions. So, I'd suggest using the Account.AccountType property for checking the account type and differentiate cases where you can use one or the other properties.

Related

Sending meeting invites from another email address

I am using tool coded in VBA to send meeting invites in bunch. meeting invite
When I use an email address, that I added to Outlook first, everything works.
When I use another email address that I added to Outlook after it, I get this message:
"Sending account 'invitation#gmail.com' not found."
I paste the email address, invitation#gmail.com, from which I want to send meeting invitations in sh.cell(1,2).
The part of the code that is giving me the message:
Dim currentAccount As Outlook.Account
Dim acc As Outlook.Account
For Each currentAccount In AppOutlook.Session.Accounts
If currentAccount.SmtpAddress = sht.Cells(1, 2) Then
Set acc = currentAccount
FoundAccount = True
Exit For
End If
Next
If Not FoundAccount Then
MsgBox "Sending account '" & sht.Cells(1, 2) & "' not found."
Exit Sub
End If
How can I set up invitation#gmail.com as my current account?
I can provide the rest of the code, but my assumption is that I have to set up something in Outlook.
Your code indicates the email address is not an account.
Outlook documentation typically refers to email addresses / mailboxes as accounts.
Outlook VBA requires accounts to be added like this:
Add an email account to Outlook.

Activate Out of Office reply

I would like to automate my Out of Office based on the days that I'll be out on a biweekly basis. I don't have access to the Exchange server that hosts our Outlook.
I set an Outlook rule to send a automatic reply on Monday and apply VBA code to disable this rule when I'm in the office (with Outlook open). This is not an elegant way because the rule sends a reply to the user repeatedly every time an email is sent to me.
How can I activate my Out of Office reply on Outlook 2010 using VBA?
Here are the two resources I used:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_win10/set-up-recurring-out-of-office-auto-reply-for/71dd1fef-ba99-4a2b-be72-7d509e8848eb
https://superuser.com/questions/292426/outlook-2010-how-to-turn-out-of-office-on-automatically-when-outlook-is-closed
This is the script I have in "ThisOutlookSession" to enable/disable the rule, "HomeTime" containing my Out Of Office-like message.
Option Explicit
Private Sub Application_Quit()
SetRuleEnabled True
End Sub
Private Sub Application_Startup()
SetRuleEnabled False
End Sub
Private Sub SetRuleEnabled(ByVal bEnable As Boolean)
Dim oSession As Outlook.NameSpace
Dim oRule As Outlook.Rule
Dim oRules As Outlook.Rules
Dim oPA As Outlook.PropertyAccessor
Set oSession = Application.Session
Set oRules = oSession.DefaultStore.GetRules()
Set oPA = oSession.DefaultStore.PropertyAccessor
'*** If the Out-Of-Office is already on (eg. holidays, sick leave etc.)
'*** then it might be best to force this rule permanently off
If oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B") Then
bEnable = False
End If
For Each oRule In oRules
If oRule.Name = "HomeTime" Then
oRule.Enabled = bEnable
oRules.Save
Exit For
End If
Next
End Sub
I am assuming you've checked out Om3r's link, but I have a less elegant, if not clunky solution. I once created a VBA out of office message as a practical joke (just to avoid someone). I could not find the code, but here is a summary of what you'd need to do.
(Outlook has to be running for this to work)
Create a public boolean variable, for example OutofOffice, set by a ribbon/QAT button or on a schedule.
Create and save a draft email with your message (e.g. "I am out of the office...")
Use or set up an Inbox_ItemAdd event listener, and for each incoming email:
Reply to it,
concatenating "Auto-Reply " to the subject line
Retrieve the draft email and concatenate your draft message body to the reply body (and you'll have to figure out HTMLBody versus (non-HTML) Body. Something like:
OutMail.HTMLBody = OutMail.HTMLBody & ObjDraft.HTMLBody
or you might save your OOO message as a public static string (instead of a draft email).
Send the email
as you mentioned, you don't want to repeatedly send this to people who sent you additional emails during this period. I would probably add the sender's email address (after passing to a GetSMTPAddress type function) to an array. I'd add a IsInArray type function to check each new sender (to see if they were emailed already). This array would be erased by your procedure called when you click that control again to turn off the OOO reply.
If this worked for you, of course you could create a userform to edit the OOO message and set the schedule (day of week or specific dates).

Outlook VBA .SendUsingAccount when no account is available

Story:
In Outlook App there is one default/user account and also additional inbox, calendar (let's call it 2nd_Account) etc. added (visible) as folders based on the Exchange Server permission.
I need to programmatically create new appointment item and send it on behalf of 2nd_Account. Problem is that in Application.Session there is only 1 account (the default one).
If done manually by user, then item created from 2nd_Account olCalendar folder is SendOnBehalf of the 2nd_Account, even though it is not included in Accounts collection. Any ideas, please?
Thank you
Create the appointment in the Calendar folder of that account - use Store.GetDefaultFolder instead of Namespace.GetDefaultFolder.
Here's how I browse through the installed accounts in Outlook:
Set MAPISession = objOutlook.Application.Session 'Get the MAPI Outlook session
Dim WantedAccount as String ' Set to preferred account name
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
For Each Account In MAPISession.Accounts
If Account = WantedAccount Then
.SendUsingAccount = Account
Exit For
End If
Next

Sending email from Office 365 non-default email addresses connected to a single account

VBA created emails are sent successfully, however I can only send from the email address of the default account.
As I have a few Office 365 connected email addresses, in Outlook 2016 I am able to manually select the 'FROM' out of the list of available connected email addresses.
In Outlook VBA, there is only one account (the exchange of Office 365), so using SendUsingAccount will find the one account.
SentOnBehalfOfName bounces on security issues. Is it possible to define the 'FROM' email address?
Yes, you can, but you need to create the different accounts in Outlook. When done, use this code excerpt:
Dim SendAccount As String
SendAccount = "MyDesiredAccountName"
Set MAPISession = objOutlook.Application.Session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
'**********************************************
'* Find desired from-account
'* If not found, use default
'*
For Each Account In MAPISession.Accounts
If Account = SendAccount Then
MAPIMailItem.SendUsingAccount = Account
Exit For
End If
Next
Is this enough to get you started?

Accessing "From:" from VBA in Outlook 2010

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