Designating the from email - vba

Is there a way of knowing why my email doesn't exist in my Outlook?
I am automating an email send but I cannot change my from email to my desired email.
I have two inboxes setup.
I run the below code and my account is not existing, although I can manually switch to it in the email display.
Sub DoesAccountExist()
Dim OLook As Object
Set OLook = CreateObject("Outlook.Application")
If GetAccountOf("myemail#email.com", OLook) Is Nothing Then
MsgBox "Account doesn't exist"
End If
End Sub

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.

Control contents of email address fields

I want to send the body of a Word document as an email from MS Word 2016.
I want the user to select recipients from the address book. I want them to only be put in the BCC field.
How do I monitor the to/from/CC/BCC fields for changes, and then move those changes to BCC?
The documentation indicates the use of Inspectors, but nothing specific about accessing the contents of these fields.
I have two approaches:
open a new Outlook mail item, load the contents of the Word file to it, and then try to monitor the fields that way.
send directly from Word using the Quick Access Toolbar option "Send to Mail Recipient".
I don't know if that is an option based on what I was reading and if those fields are accessible via VBA.
Code example of what I have so far:
Sub SendDocumentInMail()
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
'Set the recipient for the new email
.To = "recipient#mail.com"
'Set the recipient for a copy
.CC = "recipient2#mail.com"
'Set the subject
.Subject = "New subject"
'The content of the document is used as the body for the email
.Body = ActiveDocument.Content
.Send
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
It seems you are interested in the SelectNamesDialog object which displays the Select Names dialog box for the user to select entries from one or more address lists, and returns the selected entries in the collection object specified by the property SelectNamesDialog.Recipients.
The dialog box displayed by SelectNamesDialog.Display is similar to the Select Names dialog box in the Outlook user interface. It observes the size and position settings of the built-in Select Names dialog box. However, its default state does not show Message Recipients above the To, Cc, and Bcc edit boxes.
The following code sample shows how to create a mail item, allow the user to select recipients from the Exchange Global Address List in the Select Names dialog box, and if the user has selected recipients that can be completely resolved, then send the mail item.
Sub SelectRecipients()
Dim oMsg As MailItem
Set oMsg = Application.CreateItem(olMailItem)
Dim oDialog As SelectNamesDialog
Set oDialog = Application.Session.GetSelectNamesDialog
With oDialog
.InitialAddressList = _
Application.Session.GetGlobalAddressList
.Recipients = oMsg.Recipients
If .Display Then
'Recipients Resolved
oMsg.Subject = "Hello"
oMsg.Send
End If
End With
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.

Why am I not getting an email address from .SenderEmailAddress?

I am trying to create a macro in a Word form where it saves the form as a PDF, attaches it to an email, then sends the email to a predefined email address while CCing the submitter so they have a copy of the form.
I have everything working except I cannot for the life of me figure out how to get it to grab the sender email address.
Here is the macro:
Private Sub CommandButton21_Click()
Dim OL As Object
Dim EmailItem As Object
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
SubmitEmail = EmailItem.SenderEmailAddress
ActiveDocument.SaveAs2 FileName:="\\folder\folder\file.pdf", FileFormat:=wdFormatPDF
With EmailItem
.Subject = "Completed Training Selection Form"
.Body = "See Attached"
.To = "submit#test.com"
.CC = SubmitEmail
.Attachments.Add "\\folder\folder\file.pdf"
.Send
End With
Application.ScreenUpdating = True
MsgBox "Form submitted. Check your email for a copy of the form."
Set OL = Nothing
Set EmailItem = Nothing
End Sub
The macro works but it does not CC anyone. I've tried so many different ways to grab the sender address from moving the code around to stepping all the way back to the account object.
I've added SubmitEmail to the body message to confirm it is blank. Is there something more I need to do since it is a Word macro and EmailItem is an Outlook object? Could computer/network permissions affect it? The email sends just fine from the sender's account but I am at a total loss right now.
Edit: I've even done .CC = .SenderEmailAddress and still nothing.
You are adding the sender email address of the message that you just created, of course SenderEmailAddress will be "" since the message has never been submitted.
Is it supposed to be the address of the current user? Try Application.Session.CurrentUser.Address.

Sending from a shared mailbox without using sentOnBehalfOf

I'm currently using outlook.application to send mail from a shared mailbox.
I need a way to send these messages without my email address appearing on the 'from' list. It should only be the shared mailbox appearing. At the moment i'm using .sentOnBehalfOf, is there something else i should be using?
Request Send As permission.
http://social.technet.microsoft.com/Forums/office/en-US/7fd3e945-092a-461b-afa9-a126b8cc3cdd/configure-outlook-to-send-as-permissions
You should be able choose the shared account in the From field of email.
Use .SendUsingAccount to specify the shared account in VBA.
http://www.rondebruin.nl/win/s1/outlook/account.htm
Sub Which_Account_Number()
'Don't forget to set a reference to Outlook in the VBA editor
Dim OutApp As Outlook.Application
Dim I As Long
Set OutApp = CreateObject("Outlook.Application")
For I = 1 To OutApp.Session.Accounts.Count
MsgBox OutApp.Session.Accounts.Item(I) & " : This is account number " & I
Next I
End Sub
The shared account will likely be 2.
With OutMail
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
End With