Solved

Reference a variable in HTML when sending an email from Access 2013

Posted on 2014-03-12
3
598 Views
Last Modified: 2014-03-12
I have an Access (Access 2013) form which I use to send email out to about 350 people.  I put the html in a text box to send it out.  The VBA is as follows

-------------------
Private Sub cmdSend_Click()
        Dim mess_body As String
        Dim appOutLook As Outlook.Application
        Dim MailOutLook As Outlook.MailItem
        Dim rst As ADODB.Recordset
        Dim vCount
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)
        Set rst = New ADODB.Recordset
        Set rst.ActiveConnection = CurrentProject.Connection
           
        rst.CursorType = adOpenKeyset
        rst.LockType = adLockOptimistic
        rst.Source = "EmailSend"
        rst.Open options:=adCmdTable
                   
        vCount = 0
                   
            rst.MoveFirst
            Do Until rst.EOF
            Set appOutLook = CreateObject("Outlook.Application")
            Set MailOutLook = appOutLook.CreateItem(olMailItem)
            With MailOutLook
                .BodyFormat = olFormatRichText
                vEmail = rst("InternalEmail")
                vName = rst("Name")
                .To = vEmail
               
                .Subject = Me.Mess_Subject
             
                .HTMLBody = Me.mess_text
                    If Left(Me.Mail_Attachment_Path, 1) <> "<" Then
                        .Attachments.Add (Me.Mail_Attachment_Path)
                    End If
                    '.DeleteAfterSubmit = True   'This would let Outlook send th note without storing it in your sent bin
                .Send
                'txtStatus.Value = "Email Gone"
                PauseIt 30
                'txtStatus.Value = "Next one"
                rst.MoveNext
               
            End With
            vCount = vCount + 1
            txtEmailCount = vCount
            Loop
           
            'MsgBox MailOutLook.Body
            Exit Sub
           
email_error:
            MsgBox "An error was encountered." & vbCrLf & "The error message is: " & Err.Description
            Resume Error_out
Error_out:
   
End Sub
------------------------------

The name of the text box is mess_text.  I would like to be able to add the name of the person the email is going to so I need to know if you can put a variable in the html code so that each time it loops and gets the email address it can also get the name.  Here is the html:

------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html>

<title>Phi Kappa Tau</title>

<head><center>
<img src="http://www.mediafire.com/imgbnc.php/fb4efed6d4c73da74f16d97c149249714d0fef645b94f90cace8e5612696bb2d4g.jpg" border="0">
</center>
</head>

<body>
<p>
<table width="80%" height="100%" align="center">
<tr><td><font face="sans-serif">

Dear Beta Omega Alum,
<p>

<p>
Faithfully Submitted,<br>
Scott Palmer

<p>
<BR CLEAR="ALL" />
<hr>
<p>
<table width="100%" align="center">
<font face="Sans-serif">
<tr><td align="center">
<a href="mailto:Webmaster@phitaualumni.org">Webmaster</a> |
<a href="http://phitaualumni.org/index.php">Phi Tau Alumni</a> | <a href="http://phitaualumni.org/AlumniWeekend.htm">Alumni Weekend</a>

</tr></td>
</font>

</table>

</body>

</html>
----------------------------

Thanks,
Scott
0
Comment
Question by:ScootterP
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39924968
So instead of "Dear Beta Omega Alum", you'd like it to read "Dear John Smith"?

If so, then you could use a "token" in the HTML, and replace that after you get the name. It looks like your VBA code already gets the name here:

 vName = rst("Name")

If so, then change your HTML to something like this:

table width="80%" height="100%" align="center">
<tr><td><font face="sans-serif">

Dear <<name>>,
<p>

<p>

Open in new window

And then do this:

Dim sBody As STring
sBody = Me.mess_text
sBody = Replace(sBody, "<<name>>", vName)
.HTMLBody = sBody
0
 

Author Comment

by:ScootterP
ID: 39924992
Where do I put the additional code?
0
 

Author Closing Comment

by:ScootterP
ID: 39925060
I messed with it for awhile and got it to work.

Thank you very much,
Scott
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now