简体   繁体   中英

Sending an Automatic Email from Excel Using Outlook When Spreadsheet is Changed

I am having an issue getting Outlook to send an email through an Excel spreadsheet. There is no problem when I use Outlook 2013 with Gmail but when I use a previous version (2010) with a unique domain (someEmail@CreatedDomain.mil) I get the error. I want to say its because of the domain but I am not sure. Below is the code I found online.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)

    '-----AUTOMATIC EMAIL GENERATION-----------------------
    Dim answer As String

    answer = MsgBox("Saving this document will send an email to the database director and close the database. Are you sure you want to save?", _
        vbYesNo, "Save confirmation")
    '    Above code informs the user
    '    that an automated email will be sent to the database
    '    director and prompts them if they are ready to save

    'Code uses the users answer to either carryout the generated email process or to not save the changes.
    If answer = vbNo Then Cancel = True
    If Cancel = True Then Exit Sub
    If answer = vbYes Then

    'Connects to outlook and retrieves information needed to create and send the email.
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OlObjects = OutlookApp.GetNamespace("MAPI")
    Set newmsg = OutlookApp.CreateItem(olMailItem)

    'Contains the email address of the person receiving the email.
    newmsg.Recipients.Add ("roman.hope.ctr@navy.mil")
    newmsg.Subject = "Automated email from excel database" 'Sets the automated subject line to the email
    newmsg.Body = "There has been a change to the excel database from the person sending linked to this email."
    'Above code has the body of the automated email

    'sets the email to "display" in the background then sends it in the next line of code (all in the background).
    newmsg.Display
    newmsg.Send 'sends the email

    'Displays a confirmation that the email has been sent.
    MsgBox "The email has been successfully sent", , "Email confirmation"
    '    ActiveWorkbook.Save

    End If
End Sub ' End of function

I have figured the issue and it was not with the code but with the network I was on.

The network had an automatic email protection that blocked all applications from sending automated generic emails.

Thanks all for the help.

For Outlook 2010 I always use this syntax, works like a charm and doesn't even need the namespace.

Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = "somebody@something.com"
    .CC = ""
    .BCC = ""
    .Subject = "YOUR SUBJECT"
    .HTMLBody = "your text as html"
    '.Attachments.Add ("path\filema.xsl") 'uncomment for attachment
    '.Display 'uncomment for display
    .Send

End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

I have not tried this with Outlook 2013, but with 2010, this sends the email as inteded. If you have multiple mail accs in your Outlook some tweaking for choosing the right one is Probably required. It should work however since the CreateItem(0) is usually always a new mail unless Microsoft is not working as expected.

You can also change the .HTMLBody to just .Body and it should work, I just prefer html formatting for my mails, because then you can easily use attachments in the body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM