vba - 更改电子表格时使用 Outlook 从 Excel 发送自动电子邮件

标签 vba email excel outlook

我在让 Outlook 通过 Excel 电子表格发送电子邮件时遇到问题。当我将 Outlook 2013 与 Gmail 一起使用时没有问题,但是当我使用具有唯一域 (someEmail@CreatedDomain.mil) 的早期版本 (2010) 时出现错误。我想说它是因为域名,但我不确定。下面是我在网上找到的代码。

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

最佳答案

我已经找到了问题所在,这与代码无关,而与我所在的网络有关。

该网络具有自动电子邮件保护功能,可阻止所有应用程序发送自动通用电子邮件。

谢谢大家的帮助。

关于vba - 更改电子表格时使用 Outlook 从 Excel 发送自动电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31319378/

相关文章:

excel - 从主字符串中提取子字符串的 VBA 代码

vba - 如果单元格值发生更改,请将值复制到另一个工作表

python - Plotly:如何在 Excel 中嵌入完全交互式的 Plotly 图形?

email - 从辅助角色 (Azure) 发送带有 C# 附件的电子邮件

linux - 多个 crontab 作业,但其中只有一个不应发送电子邮件

Windows 更新后 Excel 2010 ActiveX 控件不再工作

vba - 页眉页脚格式和公式

c# - c#关闭excel的问题

arrays - 调用 sub 后 Excel 工作簿编译错误

c# - 发送 Gmail 电子邮件