Send Mail From access (VBA Code) (1 Viewer)

shmulikharel

New member
Local time
Today, 13:08
Joined
May 22, 2010
Messages
1
Okay, after many attempts I managed to create options for sending email via Access:

Is necessary to:
1. Outlook program installed on your computer.
2. Add in the "References" the Type "Microsoft Outlook 12.0 object library" ... See More

To Send a single email:

<StartCode>
Dim olApp As Object
Dim objMail As Object

On Error Resume Next 'Keep going if there is an error


Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open


If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance of Outlook
End If

'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)

With objMail

'Set body format to HTML
.BodyFormat = olFormatHTML
.To = "Your Mail"
.Subject = "Subject"
.HTMLBody = "Text"
.send

End With

MsgBox "Operation completed successfully"

<EndCode>‎

Thanks and good luck everyone!!
 
Last edited:

mfisk1

New member
Local time
Today, 11:08
Joined
Nov 5, 2012
Messages
1
Many thanks for posting this bit of code - Ive tried loads and this is definately a nice and simple solution that works :)

One question.....

If i wanted to insert send text with formatting in the body of the email is it simply a case of inserting the HTML Code where .HTMLBody = "Text"?

Many thanks
 

shutzy

Registered User.
Local time
Today, 11:08
Joined
Sep 14, 2011
Messages
775
i have just used this code to send 2 emails with a simple text message in the body to try it out.

i am wanting to construct a complete email with images etc. is it possible. i have already tried to use the standard html method in "HTML Body". but the vba code screen has highlighted it in RED.

Code:
.HTMLBody = "<img src="[URL]http://www.chrysalis-beauty.co.uk/images/chrysalisbanner4.png[/URL]" height="120"><br><br>"this is your loyalty points balance."<br><br>"have a nice day!!"

any ideas
 

marlan

Registered User.
Local time
Today, 13:08
Joined
Jan 19, 2010
Messages
409
Hi there two years later...

you have quotation marks in a string, the compiler thinks the string is closed, led by some other unidentified characters...
concatenate a quotation mark in a string using
Code:
char(43)
witch returns a string of a quotation mark.
 

elliotgr

Registered User.
Local time
Today, 13:08
Joined
Dec 30, 2010
Messages
67
or do this

Code:
strBody = "<html><img src=http://www.chrysalis-beauty.co.uk/im...lisbanner4.png height=120><br><br>this is your loyalty points balance.<br><br>have a nice day</html>"

and then say .HTMLBody = strBody
 

chohan78

Registered User.
Local time
Today, 11:08
Joined
Sep 19, 2013
Messages
67
you can send e-mail with out coding by using macro sendOject.
 

aindoe

New member
Local time
Today, 19:08
Joined
Feb 27, 2019
Messages
8
can I use this code under Private Sub btnSendEmail_Click() ??
I'm really new to access and i have no idea how to even add the references:(.



Okay, after many attempts I managed to create options for sending email via Access:

Is necessary to:
1. Outlook program installed on your computer.
2. Add in the "References" the Type "Microsoft Outlook 12.0 object library" ... See More

To Send a single email:

<StartCode>
Dim olApp As Object
Dim objMail As Object

On Error Resume Next 'Keep going if there is an error


Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open


If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance of Outlook
End If

'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)

With objMail

'Set body format to HTML
.BodyFormat = olFormatHTML
.To = "Your Mail"
.Subject = "Subject"
.HTMLBody = "Text"
.send

End With

MsgBox "Operation completed successfully"

<EndCode>‎

Thanks and good luck everyone!!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:08
Joined
Aug 30, 2003
Messages
36,118
Have you tried using the code? It uses late binding, shouldn't need a reference. Might need to change the constants.
 

Users who are viewing this thread

Top Bottom