Send automated e-mails from Excel to Outlook

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have some standard code in Excel which instructs Outlook to send e-mails.

Sometimes the program runs all the way to the end with no problems but at other times, it crashes.

These are the lines of code in question:

Code:
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
    
    Dim objMail As Object
    Set objMail = objOutlook.CreateItem(0)

It crashes on this line:

Code:
Set objMail = objOutlook.CreateItem(0)

with the error message:

Code:
run time error 462 the remote server machine does not exist or is unreachable

but when I drag the cursor back to this line:

Code:
Set objOutlook = CreateObject("Outlook.Application")

and press run, the program continues without any problems.

What coudl be the problem and how might I be abke to fix it?

Thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
usage:
Sent1Email "coyote@acme.com", "your order", "here is the subject"

'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

Code:
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem

On Error GoTo ErrMail

Set oApp = CreateObject("Outlook.Application")  'not this
Set oMail = oApp.CreateItem(olMailItem)

With oMail
    .To = pvTo
    .Subject = pvSubj
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
    
    .HTMLBody = pvBody
    'If Not IsNull(pvBody) Then .Body = pvBody
    
    .Display True   'show user but dont send yet
    '.Send          'send now
End With

Send1Email = True
Endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Endit
DoCmd.OutputTo acOutputReport, "rMyReport", acFormatPDF, vFile

End Function
 
Upvote 0
usage:
Sent1Email "coyote@acme.com", "your order", "here is the subject"

'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

Code:
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem

On Error GoTo ErrMail

Set oApp = CreateObject("Outlook.Application")  'not this
Set oMail = oApp.CreateItem(olMailItem)

With oMail
    .To = pvTo
    .Subject = pvSubj
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
   
    .HTMLBody = pvBody
    'If Not IsNull(pvBody) Then .Body = pvBody
   
    .Display True   'show user but dont send yet
    '.Send          'send now
End With

Send1Email = True
Endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Endit
DoCmd.OutputTo acOutputReport, "rMyReport", acFormatPDF, vFile

End Function
Thanks for the code.

I added a pause in mine and so far, it hasn't crashed.

Code:
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
 
    Application.Wait (Now + TimeValue("00:00:01"))
  
    Dim objMail As Object
    Set objMail = objOutlook.CreateItem(0)
    
    Application.Wait (Now + TimeValue("00:00:01"))
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top