need to specify 'from' email address

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

need to specify 'from' email address

Post by Peter Kinross »

I have been using the below code to send emails when an email address is double clicked on in my Db.

Code: Select all

Private Sub txtEMail_DblClick(Cancel As Integer)
Dim Msg As Outlook.MailItem
Dim appOutlook As New Outlook.Application
If Len(Nz(Me![txtEMail])) > 0 Then
    'Bring up the Email program
    Set Msg = appOutlook.CreateItem(olMailItem)
    With Msg
       .To = Nz(Me![txtEMail])
       .Body = Nz(Me![txtFirstName]) & NL()
       .Display
    End With
Else
    Beep
End If
End Sub
After many years, I am guessing, well over 10, this code suddenly sends the email from an address that I never use nor ever check.
(The NL() inserts a line break.)
Can I stipulate the 'From' address in the above code?
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78571
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: need to specify 'from' email address

Post by HansV »

Try this:

Code: Select all

Private Sub txtEMail_DblClick(Cancel As Integer)
Dim Msg As Outlook.MailItem
Dim Acc As Outlook.Account
Dim appOutlook As New Outlook.Application
If Len(Nz(Me![txtEMail])) > 0 Then
    'Bring up the Email program
    Set Acc = appOutlook.Session.Accounts("Peter Kinross")
    Set Msg = appOutlook.CreateItem(olMailItem)
    With Msg
       .To = Nz(Me![txtEMail])
       .Body = Nz(Me![txtFirstName]) & NL()
       .SendUsingAccount = Acc
       .Display
    End With
Else
    Beep
End If
End Sub
Replace "Peter Kinross" with the name of the account from which you want to send, spelled exactly as in File > Account Settings > Account Settings.
Alternatively, you can use

Code: Select all

    Set Acc = appOutlook.Session.Accounts(3)
to specify the 3rd account listed in File > Account Settings > Account Settings as the sender.
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: need to specify 'from' email address

Post by Peter Kinross »

Dang your good Hans.
Yep that works perfectly.
I found the reason for the change in account being used. Somehow the unused account had been set as the default. Your solution will overcome that if it ever happens again.
ThanksHansStamp.gif
You do not have the required permissions to view the files attached to this post.
Avagr8day, regards, Peter