title: MS Access - Automatic email templates with Outlook
body: <p>I have attempted to modify the code in <a href="https://stackoverflow.com/questions/17973549/ms-access-vba-sending-an-email-through-outlook">Sorceri's answer on StackOverflow</a>, which demonstrates how to send an email using outlook. I tried to modify this to provide a custom email template, specific to the selected entry in the database:</p>
  
  <pre><code>Private Sub Emailtemplatebtn_Click()
  
  Dim App As Outlook.Application
  Dim Mail As MailItem
  Set App = CreateObject("Outlook.application")
  
  Set Mail = oApp.CreateItem(olMailItem)
  oMail.Body = "Dear" &amp;[Forename]&amp; "," &amp;vbCrLf&amp; &amp;vbCrLf&amp; "It has been six months or longer since I last contacted you. Have there been any big gains with regard to impact?"
  oMail.Subject = &amp;[Project name]&amp; "- check up"
  oMail.To = &amp;Email&amp;
  oMail.Send
  Set oMail = Nothing
  Set oApp = Nothing
  
  End Sub
  </code></pre>
  
  <p>Unfortunately, the code doesn't work. The in-built VBA editor highlights the lines "oMail.Body", "oMail.subject", and "oMail.to" in red.</p>
  
  <p>I figure that this could be a very helpful feature, as it will save users even more time - the script/database will essentially handle all of the user's copy and pasting! </p>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <p>After using "Option Explicit", it seemed to be easier to locate why the code was failing - variables were not properly defined (I added some "o"s, and took some away). Here is the finished code:</p>
  
  <ol>
  <li><p>Put "Option Explicit" at the top of the VBA page for the form (the module for the form)</p></li>
  <li><p>Use the following code, largely corrected by Andre. </p>
  
  <p>Private Sub Emailtemplatebtn_Click()</p>
  
  <pre><code>Dim App As Outlook.Application
  Dim oMail As MailItem
  Set App = CreateObject("Outlook.application")
  
  
  Set oMail = App.CreateItem(olMailItem)
  oMail.Body = "Dear" &amp; Me![Forename] &amp; "," &amp; vbCrLf &amp; vbCrLf &amp; "It has been six months or longer since I last contacted you. Have there been any big gains with regard to impact?"
  oMail.Subject = Me![Project name] &amp; "- check up"
  oMail.To = Me!Email
  oMail.Send
  Set oMail = Nothing
  Set App = Nothing
  
  End Sub
  </code></pre></li>
  </ol>
  
  <p>Change <code>oMail.send</code> to <code>oMail.display</code> if you wish to make edits to the email before sending.</p>