How to create an HTML Email in Microsoft Access


A recent client of mine wanted me to make some enhancements to an Access database. The enhancements were all aimed at reducing the workflow. One of the issues they were encountering was at the end of the workflow, the user clicked a button inside the Access database that opened a Word file. This Word file would then walk the user through doing a Mail Merge from within Word to get elements out of the database and then create e-mails in Microsoft Outlook.

The problem was there were quite a few steps involved and at the end of it all, the user had to go back into the database and wipe out some temp tables to be able to restart the process the next day. If they got up from their desk and then came back – it was pretty easy to forget where they were in the workflow and could miss steps.

The client wanted to know if there was a way to do the entire mail merge process inside Access so that everything was automated and the wouldn’t have to go out to Microsoft Word. Piecing together a bunch of different scripts online, I was able to duplicate their Word template as an HTML formatted email, generated from within Access with all the appropriate data elements included in the e-mail.

Here is the outline of the code I used – I have included comments to explain what different parts are doing in the code. If you have specific questions, please let me know in the comments below.

Enjoy!


Private Sub send_mail() 'Substantiate the script

'Create application and mail objects
Dim olApp As Object 
Dim objMail As Object

'Create a query definition and set it to run a specific query.
'Change "Email Query" in square brackets to match query name in your database.
Dim qd As QueryDef
Set qd = CurrentDb.QueryDefs![Email Query]

'Create a record set and run the query defined above
Dim rst As Recordset
Set rst = qd.OpenRecordset()

'The following code loops through each record brought back by the query and
'creates an email for each record.
rst.MoveFirst
Do Until rst.EOF

strElement1 = rst![DataElement1]
strElement2 = rst![DataElement2]

rst.MoveNext

   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
    End If

    'Create e-mail item
   Set objMail = olApp.CreateItem(olMailItem)
   With objMail
   'Set body format to HTML
     .BodyFormat = olFormatHTML
     .To = "address@yourmailaddress.com"
     '.Cc = "ccaddress@yourmailaddress.com" 


Uncomment out above line to add a carbon copy e-mail address


     '.Bc = "bcaddress@yourmailaddress.com"


Uncomment out above line to add a blind copy e-mail address


    .Subject = "E-mail Message Subject Goes Here"
    .HTMLBody = "<!DOCTYPE html>"
    .HTMLBody = .HTMLBody & "<html><head><body>" 


You can keep building out the html using the same syntax of adding .HTMLBody from the line above & tacking on whatever is new:


    .HTMLBody = .HTMLBody & "<h1><u>This is an example header line</u></h1>"
    .HTMLBody = .HTMLBody & "<h2><u>This is an example header 2 line</u></h2>"
    .HTMLBody = .HTMLBody & "<table>"
    .HTMLBody = .HTMLBody & "<tr><td>Element 1</td><td>"& strElement1 & "</td></tr>"
    .HTMLBody = .HTMLBody & "<tr><td>Element 2</td><td>"& strElement2 &"</td></tr>"
    .HTMLBody = .HTMLBody & "</table>"
    .HTMLBody = .HTMLBody & "<br><br><img height=""20"" width=""684"" border=""0""
    src=""C:\Users\USERNAME\Desktop\image001.png"" style=""width: 684px; height: 20px;""/img>"


Above is an example of adding in an image to the Email HTML


    .HTMLBody = .HTMLBody & "<br><br>Email Customer Service at 
    <a href=""mailto:Support@youremailaddress.com"">Support@youremailaddress.com</a>"


Above is an example of adding a URL to the Email HTML


    .HTMLBody = .HTMLBody & "<br>or call 1-800-YOUR NUMBER HERE (Mon – Fri, 8am - 8pm Eastern)."
    .HTMLBody = .HTMLBody & "</body></html>"


By ucommentiong out the "Send" command below, emails will be sent out without allowing the user to review them first. Using the "Display" command brings up all the emails as draft emails and allows the user to review them prior to sending them.


     '.send
     .Display
   End With
Loop

End Sub

Outlook: Deleting an E-mail Account (3 of 3)


Note: These instructions are for Outlook 2010. The process is similar in other versions of Outlook, the menus are just different.

As a consultant, I move from client to client on a regular basis. When I leave a client, I want to save my e-mails and archive them in case I ever have to look back at them.

This is part three of a three-part series where I walk you through:

  1. Exporting an entire e-mail account
  2. Opening a .PST (archive) file
  3. Deleting an old e-mail account

Part 3: How to delete an entire e-mail account in Outlook.

CAUTION! Once you delete an account, there is no way to recover it! Please ensure that you have backed up all your information before doing this!

  1. In Outlook, click on the “File” tab.
  2. Click the “Account Settings” button and select the “Account Settings…” option in the drop down menu.
  3. Highlight the e-mail account you wish to permanently delete and click the “Remove” button.
  4. Follow the prompts to confirm that you want to remove it.

Let me know if you have any questions!

Outlook: Opening a PST file (2 of 3)


Note: These instructions are for Outlook 2010. The process is similar in other versions of Outlook, the menus are just different.

As a consultant, I move from client to client on a regular basis. When I leave a client, I want to save my e-mails and archive them in case I ever have to look back at them.

This is part two of a three-part series where I walk you through:

  1. Exporting an entire e-mail account
  2. Opening a .PST (archive) file
  3. Deleting an old e-mail account

Part 2: How to open an Outlook archive .PST file.

  1. In Outlook, click on the “File” tab.
  2. Select “Open” from the menu on the left.
  3. Select “Open Outlook Data File” option in the list on the right.
  4. Browse to your saved .PST file & click the “Open” button.
  5. The .PST file will show up in the bottom left of your account folder list on your main “Home” tab view.

Let me know if you have any questions!

Outlook: Exporting your E-mail Account (1 of 3)


Note: These instructions are for Outlook 2010. The process is similar in other versions of Outlook, the menus are just different.

As a consultant, I move from client to client on a regular basis. When I leave a client, I want to save my e-mails and archive them in case I ever have to look back at them.

This is part one of a three-part series where I walk you through:

  1. Exporting an entire e-mail account
  2. Opening a .PST (archive) file
  3. Deleting an old e-mail account

Part 1: How to export an entire e-mail account to an Outlook archive .PST file.

  1. To do this, in Outlook, click on the “File” tab.
  2. Select “Options” from the menu on the left.
  3. Select “Advanced” from the menu on the left.
  4. Select the “Export” option in the list on the right.
  5. Highlight the “Export to file” option & then click the “Next” button.
  6. Highlight the “Outlook Data File (.PST)” option & then click the “Next” button.
  7. Highlight the E-mail account folder that you want to archive & then click the “Next” button.
  8. Browse to where you want to save the file on your hard drive using the “Browse” button.
  9. In the options section, because this is the first time you’re creating this file, it really doesn’t matter which option you select. Click the “Finish” button.

You will now have a .PST file that contains your entire Manheim account e-mail.

Let me know if you have any questions!

Outlook Quick Steps – Improve your workflow


Quick Steps are little automated actions that you can set up in Outlook to improve your workflow. Examples of Quick Steps include:

  • Filing steps (moving, copying, deleting)
  • Change message status (read, unread, important)
  • Categorize, add to tasks & set flags
  • Hotkeys for response actions (reply, forward, etc.)
  • Hotkeys for creating new appointments
  • Hotkeys for dealing with conversation threads

In addition, you can combine actions.
For example, you can set up a hotkey which sends an e-mail to a specified folder, sets the status to important and starts a new appointment dialog window all with one key.

  1. To create a new Quick Step, select the “Create New” button in the “Quick Steps” section on the “Home” tab on the ribbon.
  2. outlook_quick_steps1

  3. Enter a name for this new shortcut in the “Name:” box.
  4. Pull down on the “Actions” drop down menu and select (for example) “Move to folder”.
  5. In the new pull down menu below, choose which folder you want to move the mail to.
  6. Select the “Add Action” button if you want to add a secondary action. Repeat steps 3 and 4 for the new action.
  7. Choose a shortcut (hotkey) combination if desired.
  8. Click “Finish”.

outlook_quick_steps2