更改电子表格后,使用Outlook从Excel发送自动电子邮件

罗马

我在让Outlook通过Excel电子表格发送电子邮件时遇到问题。当我将Outlook 2013与Gmail一起使用时没有问题,但是当我使用具有唯一域([email protected])的早期版本(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 ("[email protected]")
    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
威扎德27

对于Outlook 2010,我始终使用此语法,就像一个超级按钮一样,甚至不需要名称空间。

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 = "[email protected]"
    .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

我没有在Outlook 2013中尝试过此操作,但是在2010年中,它会按原样发送电子邮件。如果您的Outlook中有多个邮件帐户,则可能需要对选择正确的邮件帐户进行一些调整。但是它应该起作用,因为除非Microsoft无法按预期工作,否则CreateItem(0)通常通常是一封新邮件。

您还可以将.HTMLBody更改为.Body,它应该可以工作,我只喜欢将html格式设置为邮件,因为这样您就可以轻松地在正文中使用附件了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google表格-根据电子表格中的特定表格发送电子邮件

将电子邮件发送到Google电子表格中的多个地址

通过电子邮件发送表单并跟踪电子表格中的回复

将Google电子表格中的范围作为电子邮件发送(格式问题)

通过Google电子表格发送HTML时事通讯电子邮件

Google Apps脚本通过电子表格发送电子邮件

编辑Google电子表格,并弹出框确认发送的电子邮件

如何通过电子邮件发送电子表格的网址

发送包含电子表格数据的电子邮件

使用GAS功能通过电子邮件发送电子表格的有效方法?

Vba 电子邮件生成器,来自 excel 电子表格的主题

如何使用数组格式化电子表格数据中的电子邮件?

使用示例代码将电子邮件主题导入Google电子表格

如何将Outlook邮件中的数据解析为Excel电子表格

如何自动打印Excel电子表格文件?

自动刷新Excel电子表格的Python脚本

如何在一个电子邮件Google应用脚本中发送多个电子表格数据?

如何根据Google Apps脚本中的电子表格日期发送电子邮件?

如何将“电子表格中的表单”的内容发送到电子邮件地址?

通过电子邮件发送时返回空的日期字段(谷歌电子表格)

将一封电子邮件发送到Google电子表格中的多个地址

尝试将电子表格中的日期与今天的日期进行匹配并发送电子邮件

我正在尝试从电子表格中获取图表以使用Google脚本发送电子邮件,但getAs()函数无法正常工作

电子表格的Excel方程

写入Excel电子表格

合并Excel电子表格

Excel电子表格公式

Excel 电子表格宏

如何使用 AppScript 格式化电子表格中电子邮件中的链接