PDA

View Full Version : Solved: changing FROM in email



njperson1984
05-16-2008, 07:47 AM
I am using this method for sending an email from Access.
I would like to change the FROM option to default to a secondary account I have privilages for. Any way to do this? .from = does not work.




Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon

set OutMail = OutApp.CreateItem(0)
With OutMail
.to = ""

.from = "Other Email Account"

Zack Barresse
05-16-2008, 04:34 PM
Hi there and welcome to the board!

It gets tricky. Either you know the name of the account from Outlook or it's order number, but this should get you there...

Sub OLtest()
Dim OL As Outlook.Application, olMail As Outlook.MailItem, olAcct As Outlook.Account
Set OL = GetObject(, "Outlook.Application")
Set olMail = OL.CreateItem(olMailItem)
Set olAcct = OL.Session.Accounts("My Account Name")
olMail.To = "whoyouaresendingto@domain.com"
olMail.Subject = "test"
olMail.Body = "testing"
olMail.SendUsingAccount = olAcct
olMail.Display
End Sub

Of course this uses Early Binding and no error handling, assuming the application is open. Just an example to show the accounts. A full solution should be much more robust than this. Please post back if you need additional help with any of this.

HTH

njperson1984
05-22-2008, 01:46 PM
Thanks for your reply Zack.
The user i am building this for has put this requirement on hold, as they have some other functionalities they want as a high priority, but I'll let you know how it turns out.

I don't know about the account--its a corporate email set up.... I know the Alias and Display Name tho.

Zack Barresse
05-23-2008, 09:21 AM
No, they would need either the account index or account name. You could loop through the accounts on the local machine, but you'd still need some way to grab the account you want. Let us know if you do end up needing a hand with this. :yes

njperson1984
05-30-2008, 09:56 AM
I am back.

Zach, I'm having trouble getting the code that you suggested to run.

Here is what i'm now working with:

Dim OL As Outlook.Application
Dim olMail As Outlook.MailItem
Dim olAcct As Outlook.Account

Set OL = GetObject(, "Outlook.Application")
Set olMail = OL.CreateItem(olMailItem)
Set olAcct = OL.Session.Accounts("My Account Name")
olMail.To = "whoyouaresendingto@domain.com"
olMail.Subject = "test"
olMail.HTMLBody = MyMessage
olMail.SendUsingAccount = olAcct
olMail.Display

When the
Dim olAcct as outlook.account executes, I get "user defined type not defined". If I comment that line out, it will execute up to olMail.SendUsingAccount = olAcct and then give me "Object doesn't support this property or method".

Is this an issue of early binding? I referenced Microsoft Outlook 11.0 Object Library already. Is there another library I need to reference?


FWIW I am using outlook 2003 SP2

Zack Barresse
05-30-2008, 11:01 AM
Yes, it is a case of Early Binding. Try Late Binding instead...

Sub OLtest()
Dim OL As Object, olMail As Object, olAcct As Object
Set OL = GetObject(, "Outlook.Application")
Set olMail = OL.CreateItem(0)
Set olAcct = OL.Session.Accounts("My Account Name")
olMail.To = "whoyouaresendingto@domain.com"
olMail.Subject = "test"
olMail.Body = "testing"
olMail.SendUsingAccount = olAcct
olMail.Display
End Sub

njperson1984
06-02-2008, 08:46 AM
Hi Zack,

I see to have passed the early binding/late binding hurdle. Both forms of the code will work (with the exception of the accounts portion) and I am now getting all the olxxx data types as drop downs when I dim in code.

It seems however that the issue is with the accounts object. I checked on MSDN but can't seem to figure out what it is a member of. I did note in MDSN that it was added in outlook 2007. I am using outlook 2003. So this may explain why!

Do you know of methods to choose the account in earlier versions of outlook, or is there a way to use the accounts object in 2003? FYI upgrading is not an option, unfort.

Thanks for all your help,
dave

Zack Barresse
06-02-2008, 10:17 AM
Ah, must've missed you're using 2003. I don't think the Accounts were exposed to the OM in 2002 and 2003 (when that feature/function was first brought into Outlook, wasn't available in 2000). There is a funky workaround - not the SendKeys one - look here...

http://www.xtremevbtalk.com/showthread.php?t=94650 (scroll to bottom)
or
http://www.outlookcode.com/codedetail.aspx?id=889 (various workarounds)

HTH

njperson1984
06-02-2008, 01:28 PM
Hi Zack,

I was able to get the program to work to specification by setting the SentOnBehalfOfName property to the display name of the account I wanted to send from.

Thanks for your help. Can you set the title of this thread to solved?

Dave

Zack Barresse
06-02-2008, 01:53 PM
You can. Go to the Thread Tools drop down, select Mark Thread Solved, then click Perform Action. :)

peakpeak
03-23-2012, 02:35 AM
Grateful if anyone can confirm that SendUsingAccount does not work with Late Bindings, esp the mail item object.
Regards
Peter

Zack Barresse
04-24-2012, 08:39 PM
It should work. If it's in the OM in Early Binding, it will work in Late Binding. You just don't have access to using the built-in data types and must pass their numerical references.

peakpeak
04-25-2012, 03:06 AM
Yes, it works now. Thanks for your reply
Regards
Peter