vba Excel宏发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/668255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:17:47  来源:igfitidea点击:

Excel Macro send email

excelemailexcel-vbavba

提问by Oleg Tarassov

i have a report that i would like to send via excel. it will include the recipitents, subject and the information in the body. actually it could copy the cells in question. what i did so far is create a button and assign a macro to it with this code:

我有一份报告,我想通过 excel 发送。它将包括收件人、主题和正文中的信息。实际上它可以复制有问题的单元格。到目前为止我所做的是创建一个按钮并使用以下代码为其分配一个宏:

Private Sub CommandButton1_Click()
 Application.Dialogs(xlDialogSendMail).Show arg1:=Sheets("Sheet1").Range("E3"), _
                      arg2:=Sheets("Sheet1").Range("E7")

End Sub

the problem is that this command sends the workbook as attachment.

问题是此命令将工作簿作为附件发送。

can someone help me with the code that will allow me to do this.

有人可以帮助我使用允许我执行此操作的代码吗?

thanks a million!

太感谢了!

cheers

干杯

采纳答案by Jon Fournier

Set a reference to the "Microsoft Outlook xx.x Object Library" and you can use this code as an example of what to do to build or send an email:

设置对“Microsoft Outlook xx.x 对象库”的引用,您可以使用此代码作为构建或发送电子邮件的示例:

As it is it will just display the email without sending. You can comment out the .display line and uncomment the .send to just send it.

因为它只会显示电子邮件而不发送。您可以注释掉 .display 行并取消注释 .send 以发送它。

Sub EmailFromExcel()
    On Error GoTo PROC_EXIT
    Dim OL As New Outlook.Application

    Dim olMail As Outlook.MailItem
    Set olMail = OL.CreateItem(olMailItem)

    Dim SrcSheet As Excel.Worksheet
    Set SrcSheet = Sheets("Sheet1")

    With olMail
        .To = SrcSheet.Range("E3").Text
        .Subject = SrcSheet.Range("E7").Text
        .Body = SrcSheet.Range("E12").Text
        .Display vbModal
        '.Send
    End With

 PROC_EXIT:
    On Error GoTo 0
    OL.Quit
    Set OL = Nothing
End Sub

回答by Simon P

Add a reference to the outlook com library; then you can copy/paste values & formatting what you need to a new sheet, then copy that into the outlook mail.

添加对outlook com库的引用;然后您可以将值和格式复制/粘贴到新工作表中,然后将其复制到 Outlook 邮件中。