Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBA to send email via outlook. Sendusingaccount

484 views
Skip to first unread message

Boon

unread,
Apr 19, 2010, 10:37:03 AM4/19/10
to
Hello,

I have been able to write a code that will send email via outlook. The next
improvement I would like to do is to select the account for outgoing email.
On my outlook I have 2 email accounts. (My personal account which is the
default. Another account is the shared account and it is the one I want my
VBA code using it.)

I found that there is a function Sendusingaccount and don't know how to use
it.

I would appreciate your help.

thanks so much,
Boon


Daniel Pineault

unread,
Apr 19, 2010, 2:38:01 PM4/19/10
to
You should ask this question in an Outlook Forum. You are more likely to get
a quicker answer.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.

"Boon" wrote:

> .
>

Mark Andrews

unread,
Apr 19, 2010, 3:00:10 PM4/19/10
to
Boon,

I wrote the following code (no guarantees, I'm testing it out with a client
in Singapore right now).

The top portion tries to match the outlook account (I think I found a code
example on microsoft's site somewhere).

HTH,
Mark Andrews
RPT Software
http://www.rptsoftware.com
http://www.donationmanagementsoftware.com


Public Sub CreateEmail(strFrom As String, strTo As String, strCC As String,
strBCC As String, strSubject As String, strTextOrHTML As String, strBodyText
As String, strBodyHTML As String)
On Error GoTo Err_CreateEmail
Dim OlApp As Outlook.Application
Dim olAccounts As Outlook.Accounts
Dim olAccount As Outlook.Account
Dim olAccountTemp As Outlook.Account
Dim olMail As MailItem
Dim FoundAccount As Boolean

Set OlApp = New Outlook.Application
FoundAccount = False

'loop through and find Outlook account based on from email address
Set olAccounts = OlApp.Application.Session.Accounts
For Each olAccountTemp In olAccounts
If (olAccountTemp.smtpAddress = strFrom) Then
Set olAccount = olAccountTemp
FoundAccount = True
Exit For
End If
Next


If (FoundAccount) Then
Set olMail = OlApp.CreateItem(olMailItem)
With olMail
.SendUsingAccount = olAccount
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = strSubject
If (strTextOrHTML = "HTML") Then
.BodyFormat = olFormatHTML
.Body = strBodyText
.HTMLBody = strBodyHTML
Else
.BodyFormat = olFormatPlain
.Body = strBodyText
End If
' .Attachments.Add filename
.Display
End With
Else
MsgBox "Could not find the appropriate Outlook account for email
address: " & strFrom & ". Contact RPT Software for assistance if needed.",
vbOKOnly, "Outlook error retrieving SMTP account"
End If

Exit_CreateEmail:
Set olMail = Nothing
Set olAccount = Nothing
Set OlApp = Nothing
Exit Sub

Err_CreateEmail:
MsgBox Err.Description
Resume Exit_CreateEmail
End Sub


"Boon" <boonyawat....@cnh.com> wrote in message
news:uN4LI383...@TK2MSFTNGP04.phx.gbl...

Lee Taylor-Vaughan

unread,
Apr 20, 2010, 10:06:13 AM4/20/10
to
Mark, You rock! works great!
thanks a ton buddy.. i've been trying to figure this out for ages!!!
Lee

"Mark Andrews" <mandrews__...@rptsoftware.com> wrote in message
news:Ojdw5J$3KHA...@TK2MSFTNGP06.phx.gbl...

Lee Taylor-Vaughan

unread,
Apr 20, 2010, 10:29:33 AM4/20/10
to
one question.
How can i populate a combo box with the list of outlook SMTP accounts on a
form?
thanks
lee

"Mark Andrews" <mandrews__...@rptsoftware.com> wrote in message
news:Ojdw5J$3KHA...@TK2MSFTNGP06.phx.gbl...

Mark Andrews

unread,
Apr 20, 2010, 12:42:40 PM4/20/10
to
Your second wish:
Here's some code in the form_load event of my email form:

'loop through available Outlook acoount and populate ComboFrom


Set OlApp = New Outlook.Application

For Each oAccount In OlApp.Session.Accounts
If oAccount.AccountType = olPop3 Then
Me.ComboFrom.AddItem oAccount.smtpAddress
End If
Next

If (Me.ComboFrom.ListCount < 1) Then
MsgBox "We cannot find any Outlook email accounts, so you cannot use
this email feature. Contact RPT Software for assistance if needed.",
vbOKOnly, "Error finding Outlook email account(s)"
DoCmd.Close
Else
Me.ComboFrom = Me.ComboFrom.ItemData(0)
End If

--

"Lee Taylor-Vaughan" <le...@CcOoMmCcAaSsTt.net> wrote in message
news:6D55EE62-91D8-477A...@microsoft.com...

Boon

unread,
Apr 20, 2010, 4:04:54 PM4/20/10
to
Hi,

Thanks Mark for your suggestion.

I am still stucked... It seems like the code you provided only select the
account type (exchange, live meeting,...)

In outlook2007, you can select the From Address. I can select the shared
email account to be sent from. For instance, my work email is
Bo...@company.com. The shared email address (the one I wish to send from) is
Share...@company.com. And says my personal email is Bo...@gmail.com.

From my outlook, when I send email, the default From Address is
Bo...@company.com. I can change the From address to Share...@company.com.
But I cannot change it to Bo...@gmail.com. This makes sense since the gmail
account is not on the company network..

Now, from Access, I would like to setup so that the email will be sent from
Share...@company.com.

The reason I would like to do this is that I don't want to use my company
email to send emails to several people. I don't want them to reply to my
work email. I want them to reply to the shared email and thus I think I need
to send it from Shared email account.


thanks for your help.
Boon

"Mark Andrews" <mandrews__...@rptsoftware.com> wrote in message

news:%23JFsuhK...@TK2MSFTNGP05.phx.gbl...

Boon

unread,
Apr 20, 2010, 4:08:32 PM4/20/10
to
Minutes after I replied I found out the solution!

It is pretty simple. Just set the .SentOnBehalfOfName ="
Share...@company.com"

thanks!!

"Boon" <boonyawat....@cnh.com> wrote in message

news:OHnf$SM4KH...@TK2MSFTNGP04.phx.gbl...

Mark Andrews

unread,
Apr 20, 2010, 5:18:25 PM4/20/10
to
Glad you figured it out.
Mark

"Boon" <boonyawat....@cnh.com> wrote in message

news:uU4bBVM4...@TK2MSFTNGP05.phx.gbl...

Lee Taylor-Vaughan

unread,
Apr 23, 2010, 9:21:55 AM4/23/10
to
Mark, You ROCK!
Thanks a ton.. works great!
Lee


"Mark Andrews" <mandrews__...@rptsoftware.com> wrote in message

news:%23JFsuhK...@TK2MSFTNGP05.phx.gbl...

0 new messages