title: Runtime error 287. Sending Emails through Outlook using VBA in Access
body: <p>I have a case where one user would like to receive notifications of when deliveries are added to an Access Database. The simplest method I could think of was to set up an automated email via Outlook 2010. The following code is what I have in place:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim oApp As Outlook.Application
  Dim oMail As MailItem
  Dim varDnoteRef2 As String
  Dim varuser As String
  varuser = DLookup("[Employee_Name]", "employees", "[Employee_ID]=" &amp; TempVars!gloggedin)
  varDnoteRef2 = DLast("Supplier_Dnote_Ref", "Supplier_Dnotes")
  
  Set oApp = CreateObject("Outlook.application")
  Set oMail = oApp.CreateItem(olMailItem)
  oMail.Body = "A Delivery Note has been added to the Database by " &amp; varuser &amp; " on " &amp; Now() &amp; "." &amp; _
               vbNewLine &amp; "Delivery Note: " &amp; varDnoteRef2
  oMail.Subject = "Automatic Notification: Delivery Note"
  oMail.To = "Example@Email.co.uk"
  oMail.Send
  Set oMail = Nothing
  Set oApp = Nothing
  </code></pre>
  
  <p>This code works perfectly when the person who activates the code has Outlook open. However, when the user doesn't have Outlook launched the user gets an error at the <code>oMail.send</code> line. </p>
  
  <blockquote>
    <p>error # 287 error line 0 application-defined or object-defined error.</p>
  </blockquote>
  
  <p>What they get is a small icon on the desktop that is the outlook symbol with a gear/cog and message saying <code>another program or application is using outlook</code>. Which is what I expect. But why does it fail on the send?</p>
  
  <p>How can I get around this error/ is there a solution?   </p>
  
  <blockquote>
    <p>Edit (Update)</p>
  </blockquote>
  
  <p>What is more strange is that when I step through my code using F8. IT WORKS!! 
  But still not when I call the code from a form action e.g. <code>button_on_click</code></p>