Consulting

Results 1 to 13 of 13

Thread: Solved: changing FROM in email

  1. #1

    Solved: changing FROM in email

    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.




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

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

    .from = "Other Email Account"[/VBA]

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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...

    [vba]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[/vba]

    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

  3. #3
    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.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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.

  5. #5
    I am back.

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

    Here is what i'm now working with:

    [vba]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[/vba]

    When the
    [vba]Dim olAcct as outlook.account[/vba] executes, I get "user defined type not defined". If I comment that line out, it will execute up to [vba]olMail.SendUsingAccount = olAcct[/vba] 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

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yes, it is a case of Early Binding. Try Late Binding instead...

    [vba]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[/vba]

  7. #7
    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
    Last edited by njperson1984; 06-02-2008 at 09:03 AM.

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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

  9. #9
    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

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You can. Go to the Thread Tools drop down, select Mark Thread Solved, then click Perform Action.

  11. #11
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    2
    Location

    .SendUsingAccount and Late Binding

    Grateful if anyone can confirm that SendUsingAccount does not work with Late Bindings, esp the mail item object.
    Regards
    Peter

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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.

  13. #13
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    2
    Location
    Yes, it works now. Thanks for your reply
    Regards
    Peter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •