Sending email via Outlook 365

103 Views Asked by At

I am updating an old program. I noticed in the new build the email send doesn't work.

I searched for the reference. Is there a way of sending without the reference or where is the reference for Outlook 365 (local install)?

I searched for the location. I tried without references.

I lifted this code from examples on the web.

Public Function SendEmail2()
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "[email protected]"
varSubject = "Hello"
varBody = "Let's go"
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
End Function

Error message

Runtime error 2046
The command of action Send Object isn't available now

1

There are 1 best solutions below

0
Zen On

Here is a complete sub to send e-mails from MS Access using Outlook.

Sub SendOutlookMail(mTo As String, Optional mCC As String, Optional mBCC As String, Optional mSubject As String, _
   Optional mBody As String, Optional mAttach As String, Optional mSender As String, Optional SendAsHTML As Boolean = False, _
   Optional DirectSend As Boolean = False, Optional TemplatePath As String = "")
' Important: if mSender is filled, it must be the technical name of the Outlook account
   
   Dim Attachment As Variant
   Dim i          As Long
   
   On Error Resume Next
   
   If oApp Is Nothing Then
      Set oApp = CreateObject("OUTLOOK.Application")
   End If
   If Len(TemplatePath) > 0 Then
      Set oMail = oApp.CreateItemFromTemplate(TemplatePath)
   Else
      Set oMail = oApp.CreateItem(olMailItem)
   End If
   With oMail
      .To = mTo
      .SUBJECT = mSubject
      .CC = mCC
      .BCC = mBCC
      If SendAsHTML Then
         .HTMLBody = .HTMLBody & mBody
      Else
         .Body = .Body & mBody
      End If
      If Len(TemplatePath) > 0 Then
         ' In this sub I suggest that a template is always a html-template
         .HTMLBody = .HTMLBody
      End If
      .ReadReceiptRequested = False
      
      'Attachments
      Attachment = Split(mAttach, ',')
      For i = LBound(Attachment) To UBound(Attachment)
         .Attachments.Add Attachment(i)
      Next i
      
      If mSender <> "" Then
         .SentOnBehalfOfName = mSender ' Name of the Outlook account
      End If
      If DirectSend Then
         .Send
      Else
         .Display
      End If
   End With
End Sub