VB Code to send an email (1 Viewer)

taraappl

New member
Local time
Today, 00:14
Joined
Jun 28, 2011
Messages
3
Hello all. This is my very first post after searching and not finding what I need.

I need to send an email from Access 2007. I want to use an HTML file that is located at F:\Orbits Training Doc.html as the template for the email.

The subject line needs to be "Orbits Web Training"

I have a qry that is called myemailaddresses in which I need to use field "email" to populate the email address in outlook. The field will only have 1 result and not multiple.

Someone help!

If it makes it easier I could also use a .txt file as the body of the email but the HTML file is much cleaner as this is a communication that will be going out to customers.
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
Also you could simply use docmd.sendobject which can generate and send an email for your. You would have to use the openfile function to read the html file then pass that as the body before you actually use the docmd.sendobject command.
 

taraappl

New member
Local time
Today, 00:14
Joined
Jun 28, 2011
Messages
3
Thanks for the swift replies. I am really a novice when it comes to VB code. I've been searching all the sites today and some of it looks good it doesnt quite do exactly what I need it to do. I'm really looking for someone to give me the code for what I need. So to recap: I'm looking for this code to fire off when clicking on a button from a form. I would like the code to create an email in outlook that pulls in the body for that email from an html document I have saved locally. I want it to insert the email address from a query that will have a field called Email. It will always only have 1 email address, not many. The subject will always be the same and I would like to see it before it sends. It sounds simple enough to me and the code I did find today wouldnt pull in the html and it gave me an input box for my subject line and I didnt want that and couldnt figure out how to change it. Any help would really be appreciated.
 

techexpressinc

Registered User.
Local time
Today, 01:14
Joined
Nov 26, 2008
Messages
185
I do emails with this module outside of MS Outlook Exchange. From event VBA, I pass parameters building the subject line, to email and body to this famous common module.
Code:
Option Compare Database
Public Function Xmail(strTo As String, strFrom As String, strSub As String, strBody As String, Optional strAtt As String)

'[This function will let you send email messages via SMTP without involving your local email client, like Outlook.]
'[As such, there is no "sent" history, so this option would not be preferred if you need to have this history.]
'[Requires reference to Microsoft CDO 1.21 Library, though it sometimes works without.]
'[As coded, this only works for unsecured/internal SMTP servers.  However, you can add in login information should you need to.]
'[strAtt is the only optional argument and, if included, must be the full path and extension. Ex: "c:\file.txt"

Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = strSub
    objMessage.From = strFrom
    objMessage.to = strTo
    objMessage.TextBody = strBody
    objMessage.CC = "[EMAIL="Russell.Neuman@lfg.com"]Russell.Neuman@lfg.com[/EMAIL]"
If Len(strAtt) > 0 Then
    objMessage.AddAttachment strAtt
End If

'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserver[/URL]") = "XYZ.us.ad.XYZ.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserverport[/URL]") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.CC = "XYZ[EMAIL="XYZ@XYZ.com"]@XYZ.com[/EMAIL]"
objMessage.Send
 
End Function
 

techexpressinc

Registered User.
Local time
Today, 01:14
Joined
Nov 26, 2008
Messages
185
No this is not for Outlook. There is a different batch of code out on web. I did test that code a couple months ago and did get it working. But there is a Microsoft Security pop-up security message. There are patches to make it so the pop-up security does not happen, from places like www.mapilab.com, etc.
But, security is pretty tight where I am and every pc running the email generating code would need the over-ride code to work around the MicroSoft security message. I thought that was a bridge to avoid. Because, if you do install that code, what happens if there is an email virus and the MS security code would have stopped it. But you in your infinite wisdom over-rode the MS security code, management would not appreciated that.
Russ
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
Here is the code I use to work with outlook. Its pretty simple but I have found out that docmd.sendobject also works very well for sending emails out of a macro. This is in module format.

Code:
Option Compare Database
' InitOutlook sets up outlookApp and outlookNamespace.
Private outlookApp As Outlook.Application
Private outlookNamespace As Outlook.Namespace
 
Sub InitOutlook()
    ' Initialize a session in Outlook
    Set outlookApp = New Outlook.Application
    
    'Return a reference to the MAPI layer
    Set outlookNamespace = outlookApp.GetNamespace("MAPI")
    
    'Let the user logon to Outlook with the
    'Outlook Profile dialog box
    'and then create a new session
    outlookNamespace.Logon , , True, False
End Sub
 
Sub Cleanup()
    ' Clean up public object references.
    Set outlookNamespace = Nothing
    Set outlookApp = Nothing
End Sub
 
Function SendEmail()
Dim mailItem As Outlook.mailItem
InitOutlook
Set mailItem = outlookApp.CreateItem(olMailItem)
mailItem.To =
mailItem.Subject =
mailItem.Body =
mailItem.Save
mailItem.Close olSave
Set mailItem = Nothing
Cleanup
End Function

What it does is produce the emails and saves them in your drafts box. Then I made some code to send out all drafts for outlook. This code would go into outlook.

Code:
Public Sub SendDrafts()
Dim lDraftItem As Long
Dim myOutlook As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolders As Outlook.Folders
Dim myDraftsFolder As Outlook.MAPIFolder
 
'Send all items in the "Drafts" folder that have a "To" address filled 'in.
'Setup Outlook
Set myOutlook = Outlook.Application
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolders = myNameSpace.Folders
'Set Draft Folder.  This will need modification based on where it's 'being run.
Set myDraftsFolder = myFolders("Put your Mailbox name as it appears here").Folders("DRAFTS")
'Loop through all Draft Items
For lDraftItem = myDraftsFolder.Items.Count To 1 Step -1
'Check for "To" address and only send if "To" is filled in.
If Len(Trim(myDraftsFolder.Items.Item(lDraftItem).To)) > 0 Then
'Send Item
myDraftsFolder.Items.Item(lDraftItem).Send
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
DoEvents: DoEvents: DoEvents
End If
Next lDraftItem
'Clean-up
Set myDraftsFolder = Nothing
Set myNameSpace = Nothing
Set myOutlook = Nothing

End Sub

It may not be perfect but I manage 1600 some emails a day with it. And yes it does give a security prompt at the beginning but all you have to do is say give access to outlook for 10 minutes and no more security prompts for the building of the emails.

L8r
 

techexpressinc

Registered User.
Local time
Today, 01:14
Joined
Nov 26, 2008
Messages
185
Thanks for the Outlook code - I might still got thaty way. When the email id is outside of the company address book, this pop-up error message is occurring.

"The server rejected one or more addressses. The server response was: 550 5.7.1 Unable to relay for emailid@gmail.com "
 

LukeChung-FMS

President, FMS Inc
Local time
Today, 01:14
Joined
Nov 20, 2008
Messages
17
If you're not a programmer and want to take advantage of combining Microsoft Access with HTML emails, consider our Total Access Emailer program.

It will let you send personalized emails to everyone in your list (from a table or query) and even attach filtered reports as PDF files so each recipient only gets their data. It can even zip up all your attachments and add a password for extra security.

If you have an HTML file that you want to send, it's also smart enough to embed the graphics for the file in the message if the graphic files are in the same folder.

More information and a free demo are available from the fmsinc.com web site.

Total Access Emailer runs as an interactive Microsoft Access add-in. It also has a programmatic VBA interface and royalty-free runtime library if you want to include it in your application and distribute it.
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
Rejecting an address just means it could not find the address you are trying to send to. Or you do not have permissions to send to that individual. Either way the code sounds like its working as long as the email address is right.
 

csvaasan

New member
Local time
Today, 10:44
Joined
Aug 18, 2012
Messages
3
Hi thechazm,

I am not a programmer; so, can you pls explain in detail about how to write and run this FIRST macro (produce the emails and saves them in your drafts box) in MS Access 2010...

Note: The second part of your answer (sending all emails of Draft Folder at once in Outlook 2010) is working fine for me.

Code:
Option Compare Database
' InitOutlook sets up outlookApp and outlookNamespace.
Private outlookApp As Outlook.Application
Private outlookNamespace As Outlook.Namespace
 
Sub InitOutlook()
    ' Initialize a session in Outlook
    Set outlookApp = New Outlook.Application
    
    'Return a reference to the MAPI layer
    Set outlookNamespace = outlookApp.GetNamespace("MAPI")
    
    'Let the user logon to Outlook with the
    'Outlook Profile dialog box
    'and then create a new session
    outlookNamespace.Logon , , True, False
End Sub
 
Sub Cleanup()
    ' Clean up public object references.
    Set outlookNamespace = Nothing
    Set outlookApp = Nothing
End Sub
 
Function SendEmail()
Dim mailItem As Outlook.mailItem
InitOutlook
Set mailItem = outlookApp.CreateItem(olMailItem)
mailItem.To =
mailItem.Subject =
mailItem.Body =
mailItem.Save
mailItem.Close olSave
Set mailItem = Nothing
Cleanup
End Function
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
It might be later on today because of my work load or even tomorrow but I'll try to explain it soon.
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
Code:
Option Compare Database
' InitOutlook sets up outlookApp and outlookNamespace.
Private outlookApp As Outlook.Application
Private outlookNamespace As Outlook.Namespace
 
Sub InitOutlook()
    ' Initialize a session in Outlook
    ' [COLOR=red]This starts outlook or just uses the currently open one.[/COLOR]
    Set outlookApp = New Outlook.Application
    
    'Return a reference to the MAPI layer
    ' [COLOR=red]Finds the outlook namespace which it will need to access the object[/COLOR]
    Set outlookNamespace = outlookApp.GetNamespace("MAPI")
    
    'Let the user logon to Outlook with the
    'Outlook Profile dialog box
    'and then create a new session
    outlookNamespace.Logon , , True, False
End Sub
 
Sub Cleanup()
    ' Clean up public object references.
    Set outlookNamespace = Nothing
    Set outlookApp = Nothing
End Sub
 
Function SendEmail()
Dim mailItem As Outlook.mailItem ' [COLOR=red]This creates the object in memory and is defining what type of object we want to work with.[/COLOR]
InitOutlook ' [COLOR=red]This calls the sub above to open outlook or use the current one[/COLOR]
Set mailItem = outlookApp.CreateItem(olMailItem) ' [COLOR=#ff0000]Uses the newly opened outlook or existing one and assigns it to our object that we opened earlier so we can work with it in our current function.[/COLOR]
mailItem.To = ' [COLOR=#ff0000]Sets To field in a message. So the email address should be supplied here.[/COLOR]
mailItem.Subject = ' [COLOR=red]Sets the subject line which the user normally sees first in outlook.[/COLOR]
mailItem.Body = ' [COLOR=red]Sets the contents of the email.[/COLOR]
mailItem.Save ' [COLOR=red]When you call save on a an outlook mail item it automatically saves in your drafts folder.[/COLOR]
mailItem.Close olSave ' [COLOR=red]This closes the mail item and forces it to save just in case the save didn't finish.[/COLOR]
Set mailItem = Nothing ' [COLOR=red]Clears the object from your computer memory so we don't have memory leaks.[/COLOR]
Cleanup ' [COLOR=red]This is the same principle as the one above as its just cleaning up the memory objects and taking them out of your computers memory.[/COLOR]
End Function

If you have specific questions then let me know.
 

csvaasan

New member
Local time
Today, 10:44
Joined
Aug 18, 2012
Messages
3
Code:
Option Compare Database
' InitOutlook sets up outlookApp and outlookNamespace.
Private outlookApp As Outlook.Application
Private outlookNamespace As Outlook.Namespace
 
Sub InitOutlook()
    ' Initialize a session in Outlook
    ' [COLOR=red]This starts outlook or just uses the currently open one.[/COLOR]
    Set outlookApp = New Outlook.Application
    
    'Return a reference to the MAPI layer
    ' [COLOR=red]Finds the outlook namespace which it will need to access the object[/COLOR]
    Set outlookNamespace = outlookApp.GetNamespace("MAPI")
    
    'Let the user logon to Outlook with the
    'Outlook Profile dialog box
    'and then create a new session
    outlookNamespace.Logon , , True, False
End Sub
 
Sub Cleanup()
    ' Clean up public object references.
    Set outlookNamespace = Nothing
    Set outlookApp = Nothing
End Sub
 
Function SendEmail()
Dim mailItem As Outlook.mailItem ' [COLOR=red]This creates the object in memory and is defining what type of object we want to work with.[/COLOR]
InitOutlook ' [COLOR=red]This calls the sub above to open outlook or use the current one[/COLOR]
Set mailItem = outlookApp.CreateItem(olMailItem) ' [COLOR=#ff0000]Uses the newly opened outlook or existing one and assigns it to our object that we opened earlier so we can work with it in our current function.[/COLOR]
mailItem.To = ' [COLOR=#ff0000]Sets To field in a message. So the email address should be supplied here.[/COLOR]
mailItem.Subject = ' [COLOR=red]Sets the subject line which the user normally sees first in outlook.[/COLOR]
mailItem.Body = ' [COLOR=red]Sets the contents of the email.[/COLOR]
mailItem.Save ' [COLOR=red]When you call save on a an outlook mail item it automatically saves in your drafts folder.[/COLOR]
mailItem.Close olSave ' [COLOR=red]This closes the mail item and forces it to save just in case the save didn't finish.[/COLOR]
Set mailItem = Nothing ' [COLOR=red]Clears the object from your computer memory so we don't have memory leaks.[/COLOR]
Cleanup ' [COLOR=red]This is the same principle as the one above as its just cleaning up the memory objects and taking them out of your computers memory.[/COLOR]
End Function
If you have specific questions then let me know.


Hi thechazm,
First, my sincere apologies for the much delayed response to your reply;

Can you please consider me as a novice with MS Access and give step-by-step instructions on How to use your Code...
 

thechazm

VBA, VB.net, C#, Java
Local time
Today, 01:14
Joined
Mar 7, 2011
Messages
515
Please understand I do not have enough time to give a complete tutorial on how to use or work with access right now. There are plenty of youtube videos and other resources out there that can get you to where you understand what I did in the code. If you have a specific question then I dont mind answering it. Let me know.

TheChazm
 

Users who are viewing this thread

Top Bottom