Outlook Accounts

kitty77

Registered User.
Local time
Today, 07:37
Joined
May 27, 2019
Messages
719
I'm using the following code to send emails. Works just fine.
Is it possible to have it send it through a specific Outlook account? I have several accounts and have to change back and forth a lot.

Thanks!

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)

With MailOutLook
.To = [Memailsent]
.CC = [Memailsent1]
' .bcc = ""
.Attachments.Add "Attachement.pdf"
.Subject = "Subject"
.Body = "Body"
' .Send
.Display
End With
 
I see but how and what do I need in my code to make it work?
 
No. I don't want to change everything. Was hoping to add to mine? So, maybe I can just add what I need from yours?
 
No. I don't want to change everything. Was hoping to add to mine? So, maybe I can just add what I need from yours?
And I was hoping by trying out the sample code, you will understand how to use it to apply to your own code. I wasn't saying to change or replace your code in any way. All I'm saying is to separately try the sample code to see how it works. Then, perhaps you'll be able to see what you need to add to your code after seeing how it works on its own. If not, we will still be here to help you.
 
The only trick with "Send Using Account" is that you must have permission to use that account if it is in the domain where you are working. If you are an admin or if you are marked as a proxy for that account, you can send using it.
 
You might want to look at the code in the following links, the other one might be a bit misleading as it was searching for a certain account type instead of an account email:

Cheers,
 
So, using my code here, what do I need to add that will make it select a certain email account?

Thanks!

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)

With MailOutLook
.To = [Memailsent]
.CC = [Memailsent1]
' .bcc = ""
.Attachments.Add "Attachement.pdf"
.Subject = "Subject"
.Body = "Body"
' .Send
.Display
End With
 
Ok, I think I got what I need using the .SentOnBehalfOfName = "info1@testing.com.com"

How can I make it check if that account exists on that PC?

If that PC has info1@testing.com in outlook, use .SentOnBehalfOfName = "info1@testing.com"
If that PC has info2@testing.com in outlook, use .SentOnBehalfOfName = "info2@testing.com"

Some PC's have different accounts. So, need to have different .SentOnBehalfOfName

Hope I'm being clear?
Perhaps, you could loop through the Account Collection to check each account available.

 
Maybe something like this?
Code:
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim oAccount as Outlook.Account


Set appOutLook = CreateObject("Outlook.Application")

For Each oAccount In appOutLook.Session.Accounts
   If oAccount = ""info1@testing.com" Or oAccount = ""info2@testing.com"  Then 'you could use a table to store user info and use dLookup to extract it
        Set MailOutLook = CreateItem(olMailItem)
        MailOutLook.SendUsingAccount = oAccount
   End If
Next

'Set MailOutLook = appOutLook.CreateItem(olMailItem)

With MailOutLook
.To = [Memailsent]
.CC = [Memailsent1]
' .bcc = ""
.Attachments.Add "Attachement.pdf"
.Subject = "Subject"
.Body = "Body"
' .Send
.Display
End With

Cheers,
 
I was hoping for something more simple using my code and .SentOnBehalfOfName
 
Just not sure how to do the dlookup? Maybe an example?
 
Have you tried to use SendOnBehalfOfName? What did you come up with? Maybe you try to do it yourself and post back if you get stuck. The idea behind the dLookup is to have a table having a list of users\computers and the associated Outlook account to use for each; then you look up the current user\computer in the table and retrieve the corresponding Outlook account.

Cheers,
 
Yes, I have tried SendOnBehalfOfName. It works just fine. But PC 1 has a different Outlook account then PC 2.
So, I need to be able to have it have different sends? That way, I know who sent it.

Does that make sense?
 
This is what I have so far. It works fine, just need to have it use one or the other SentOnBehalfOfName.
Depending on what PC has that specific Outlook account. Or some other way...

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)

With MailOutLook
.To = [Memailsent]
.CC = [Memailsent1]
' .bcc = ""
.Attachments.Add "Attachement.pdf"
.Subject = "Subject"
.Body = "Body"
.SentOnBehalfOfName = "info1@testing.com"
.SentOnBehalfOfName = "info2@testing.com"

.Display
End With
 
First of all please make sure you understand the difference between the two options (SentOnBehalfOfName vs. SendUsingAccount):

As for your specific question I have another in return :) How do you know from inside the database on which computer you are? Do you have a login form to select the current user? How many alternatives are there for the Outlook accounts? The code I gave you in post # 13 would send the message from either one depending which one was available on that computer without the need for a dLookup.

Cheers,
 
You don't know from inside the database. No login either. The two PC each have three accounts.
 

Users who are viewing this thread

Back
Top Bottom