提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如果在Excel中单击按钮,如何发送电子邮件?

假设您需要通过点击Excel工作表中的按钮来发送电子邮件,您将如何执行? 本文将介绍一种VBA方法来实现它的细节。

如果用VBA代码点击按钮,发送电子邮件


根据创建的邮件列表通过Outlook轻松发送电子邮件:

在此 发电子邮件 Kutools for Excel的实用工具可帮助用户通过基于Excel中创建的邮件列表通过Outlook发送电子邮件。 查看屏幕截图:

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

如果用VBA代码点击按钮,发送电子邮件


如果在Excel工作簿中单击命令按钮,请按照以下方式通过Outlook发送电子邮件。

1。 单击,在工作表中插入一个命令按钮 开发人员 > 插页 > 命令按钮(ActiveX控件)。 看截图:

2。 右键单击插入的命令按钮,然后单击 查看代码 从下面的右键菜单中进行截图展示。

3。 在开幕式上 Microsoft Visual Basic for Applications 窗口,请使用以下VBA脚本替换代码窗口中的原始代码。

VBA代码:如果在Excel中单击按钮,则发送电子邮件

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2017/9/14
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Body content" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2"
                  On Error Resume Next
    With xOutMail
        .To = "Email Address"
        .CC = ""
        .BCC = ""
        .Subject = "Test email send by button clicking"
        .Body = xMailBody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

笔记:

1)。 请根据需要更改电子邮件正文 xMailBody 在代码中的行。

2)。 更换 电邮地址 与收件人的电子邮件地址一致 .To =“电子邮件地址”.

3)。 根据需要指定抄送和密件抄送收件人 .CC =“” .Bcc =“” 部分。

4)。 按行更改电子邮件主题 .Subject =“通过单击按钮发送测试电子邮件”.

4。 按 其他 + Q 键同时关闭 Microsoft Visual Basic for Applications 窗口。

5。 单击关闭设计模式 开发人员 > 设计模式。 看截图:

从现在起,每次点击命令按钮时,都会自动创建一封电子邮件,其中包含指定收件人,主题和正文。 请通过点击发送电子邮件 发送 按钮。

注意:当您使用Outlook作为您的电子邮件程序时,VBA代码才起作用。


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Tina Ellis · 26 days ago
    I am running into a problem where the code works but will not pull the data from the cells I am telling it to. I know I am missing a command that tells it i want he wording in the cell to display instead of B8 to display in the email, can you help me?

    Private Sub CommandButton2_Click()
    'Updated by Extendoffice 2017/9/14
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Payroll Information for [D4] is listed below" & vbNewLine & vbNewLine & _
    "[B8] [F8]" & vbNewLine & _
    "[B9] [F9]" & vbNewLine & _
    "[B10] [F10]" & vbNewLine & _
    "[B11] [F11]" & vbNewLine & _
    "[H13] [J13]" & vbNewLine & _
    "[H14] [J14]" & vbNewLine & _
    "[H15] [J15]" & vbNewLine & _
    "[H16] [J16]" & vbNewLine & _
    "[H17] [J17]" & vbNewLine & _
    "[H18] [J19]" & vbNewLine & _
    "[H19] [J19]"

    On Error Resume Next
    With xOutMail
    .To = "payroll@jpservices.com"
    .CC = ""
    .BCC = ""
    .Subject = "Offer Accepted by " & [D4]
    .Body = xMailBody
    .Display 'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
    End Sub
  • To post as a guest, your comment is unpublished.
    marcel · 1 months ago
    Hi I used the code below and was wondering if I can send only the active sheet instead of the complete workbook I did try some code but with no luck

    I'm still to new to VBA coding

    Private Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim sAttach As String
    Dim sTo As String
    Dim sCC As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    'variable declarations for email body and attachment
    strbody = "Hi All;

    Display.
  • To post as a guest, your comment is unpublished.
    Courtney · 1 months ago
    Hi! Thank you so much for this. It's been fantastic help.

    Is it possible to send an active work sheet by pressing a button instead of the full workbook?

    Thanks!
  • To post as a guest, your comment is unpublished.
    Terence Letley · 1 months ago
    Hi, I run a spreadsheet with 80 individual bar accounts and have used VBA code on this page with much success. However in the body of the email I would like to copy and paste the specific range of cells of an account when sending so the client can have a history. Can you help with the VBA code for this?
  • To post as a guest, your comment is unpublished.
    Montuella Nang · 2 months ago
    Hi,

    I have been working on a sheet, but i can't finish it. I hope u can help me :)

    The file its self must be a xltm (template) and it must attach the sheet it self in the mail.

    And a auto signature, then i would be very happy.

    Thanks in advance /Dr. Nang
  • To post as a guest, your comment is unpublished.
    Kris · 3 months ago
    Hi,

    Is there a way to change the file name to the current date when it attaches to the email?
  • To post as a guest, your comment is unpublished.
    Grant · 3 months ago
    How can I attach the Active Worksheet to the email when I press the button?
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Hi Grant,
      Please right click the button and select View Code, then copy below code between the Private Sub and the End Sub lines. Hope I can help. Thanks for your comment.

      Dim xFile As String
      Dim xFormat As Long
      Dim Wb As Workbook
      Dim Wb2 As Workbook
      Dim FilePath As String
      Dim FileName As String
      Dim OutlookApp As Object
      Dim OutlookMail As Object
      On Error Resume Next
      Application.ScreenUpdating = False
      Set Wb = Application.ActiveWorkbook
      ActiveSheet.Copy
      Set Wb2 = Application.ActiveWorkbook
      Select Case Wb.FileFormat
      Case xlOpenXMLWorkbook:
      xFile = ".xlsx"
      xFormat = xlOpenXMLWorkbook
      Case xlOpenXMLWorkbookMacroEnabled:
      If Wb2.HasVBProject Then
      xFile = ".xlsm"
      xFormat = xlOpenXMLWorkbookMacroEnabled
      Else
      xFile = ".xlsx"
      xFormat = xlOpenXMLWorkbook
      End If
      Case Excel8:
      xFile = ".xls"
      xFormat = Excel8
      Case xlExcel12:
      xFile = ".xlsb"
      xFormat = xlExcel12
      End Select
      FilePath = Environ$("temp") & "\"
      FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
      Set OutlookApp = CreateObject("Outlook.Application")
      Set OutlookMail = OutlookApp.CreateItem(0)
      Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
      With OutlookMail
      .To = "skyyang@extendoffice.com"
      .CC = ""
      .BCC = ""
      .Subject = "kte features"
      .Body = "Please check and read this document."
      .Attachments.Add Wb2.FullName
      .Display
      '.Send
      End With
      Wb2.Close
      Kill FilePath & FileName & xFile
      Set OutlookMail = Nothing
      Set OutlookApp = Nothing
      Application.ScreenUpdating = True
  • To post as a guest, your comment is unpublished.
    marcus · 3 months ago
    I want to send the excel worksheet in the email but it doesnt send it. How do you write code to send document as email
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Hi marcus,
      The below VBA code can help you solving the problem.

      Sub SendWorkSheet()
      'Update 20190626
      Dim xFile As String
      Dim xFormat As Long
      Dim Wb As Workbook
      Dim Wb2 As Workbook
      Dim FilePath As String
      Dim FileName As String
      Dim OutlookApp As Object
      Dim OutlookMail As Object
      On Error Resume Next
      Application.ScreenUpdating = False
      Set Wb = Application.ActiveWorkbook
      ActiveSheet.Copy
      Set Wb2 = Application.ActiveWorkbook
      Select Case Wb.FileFormat
      Case xlOpenXMLWorkbook:
      xFile = ".xlsx"
      xFormat = xlOpenXMLWorkbook
      Case xlOpenXMLWorkbookMacroEnabled:
      If Wb2.HasVBProject Then
      xFile = ".xlsm"
      xFormat = xlOpenXMLWorkbookMacroEnabled
      Else
      xFile = ".xlsx"
      xFormat = xlOpenXMLWorkbook
      End If
      Case Excel8:
      xFile = ".xls"
      xFormat = Excel8
      Case xlExcel12:
      xFile = ".xlsb"
      xFormat = xlExcel12
      End Select
      FilePath = Environ$("temp") & "\"
      FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
      Set OutlookApp = CreateObject("Outlook.Application")
      Set OutlookMail = OutlookApp.CreateItem(0)
      Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
      With OutlookMail
      .To = "skyyang@extendoffice.com"
      .CC = ""
      .BCC = ""
      .Subject = "kte features"
      .Body = "Please check and read this document."
      .Attachments.Add Wb2.FullName
      .Display
      '.Send
      End With
      Wb2.Close
      Kill FilePath & FileName & xFile
      Set OutlookMail = Nothing
      Set OutlookApp = Nothing
      Application.ScreenUpdating = True
      End Sub
  • To post as a guest, your comment is unpublished.
    kaitlyn k. · 3 months ago
    Do you have to be an excel user when pushing the button to send an email? or just the person receiving the email has to be an outlook user?
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Good day,
      As the email need to be sent through Outlook after clicking the button in Excel, you must have Outlook installed in your computer to make it work.
  • To post as a guest, your comment is unpublished.
    John · 4 months ago
    Hi,

    How can I get the mail to automatically add my signature from outlook?
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Hi John,
      The below VBA code can help you solve the problem. Thanks for your comment.

      Private Sub CommandButton1_Click()
      'Updated by Extendoffice 2019/6/26
      Dim xOutApp As Object
      Dim xOutMail As Object
      On Error Resume Next
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      With xOutMail
      .Display 'or use .Send
      .To = "Email Address"
      .CC = ""
      .BCC = ""
      .Subject = "Test email send by button clicking"
      .HTMLBody = "This is a test email sending in Excel" & "
      " & .HTMLBody
      '.Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End Sub
  • To post as a guest, your comment is unpublished.
    Rasike · 6 months ago
    Hi There,
    Can anyone could help me on the following requirement?
    I have an excel sheet having two drop down lists.

    1 drop down list- list of departments

    2 drop down list- category

    What i want is when I select a department and category from the lists;(for example if I select "sales" in department and "monthly report" in category)

    I need to email the PDF version of that work sheet to the sales team with the email subject is monthly report.

    If i select "production" from the department list, the email should go to a group of people in the production.

    I appreciate if you can help me on this

    Rasike
  • To post as a guest, your comment is unpublished.
    Abi · 7 months ago
    Can I reference the value in a cell in the subject with string? Some variation of the below?

    .Subject = "New Event:" & Target.Address = "$B$38"
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Abi,
      Please change the code to .Subject = "New Event:" & [B38].
      Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Laurie · 7 months ago
    Is it possible to, instead of sending the attachment in the email, to send a hyperlink to the file (located on SharePoint)?
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Laurie,
      Can't help you with that. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Glenn · 8 months ago
    using this VBA code, can I reference cell information within the body of the email? For instance, how would I reference cell values to the code below?

    xMailBody = "Body content" & vbNewLine & vbNewLine & _
    "This is line 1" & vbNewLine & _
    "This is line 2"

    Thanks,
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Glenn,
      Please apply the following code. Thank you for your comment.
      xMailBody = [B5]
  • To post as a guest, your comment is unpublished.
    Hannah · 9 months ago
    Is it possible to add a second email button within the same worksheet? When I try to do it, it is connecting to the code from the original email button. thanks.
    • To post as a guest, your comment is unpublished.
      crystal · 8 months ago
      Hi,
      You need to assign different codes to each button.
  • To post as a guest, your comment is unpublished.
    erick · 1 years ago
    using this VBA code, can I reference cell information within the body of the email? For instance, how would I reference cell values to the code below?

    xMailBody = "Body content" & vbNewLine & vbNewLine & _
    "This is line 1" & vbNewLine & _
    "This is line 2"

    Also, using the (.TO = "email address") code. how can I have the "email address" pull in an email address from a cell in the column next to it.


    Thanks,
  • To post as a guest, your comment is unpublished.
    Reinaldo Batista · 1 years ago
    How can I attach the Active Workbook to the email when i press the button?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good day,
      Just below ".Body = xMailBody" add the following
      .Attachments.Add ActiveWorkbook.FullName
  • To post as a guest, your comment is unpublished.
    Ravin Chatelall · 1 years ago
    Hi all first off a Big thank you, this post is very helpful and works for the attachment. This works for me but no updates are saved in the sheet you have to hit the save button. I would like the attachment to have whatever is currently in the excel sheet.

    I can accomplish this by using the excel built in E-mail feature but I wanted a button as I need to hardcode the specific email address.

    So in summary I would like to know:

    I would like to know if there is a way to: after a user open the excel workbook and made edits if the button will keep the attachment with the updates?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good Day,
      The code has been optimized. Please have a try and thanks for your comment.

      Private Sub CommandButton1_Click()
      'Updated by Extendoffice 2017/9/14
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      On Error Resume Next
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      ActiveWorkbook.Save
      xMailBody = "Body content" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      With xOutMail
      .To = "Email Address"
      .CC = ""
      .BCC = ""
      .Subject = "Test email send by button clicking"
      .Body = xMailBody
      .Attachments.Add ActiveWorkbook.FullName
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End Sub
  • To post as a guest, your comment is unpublished.
    Tina · 1 years ago
    it only generates one email and will keep overwriting the contents instead of opening up multiply email drafts.
  • To post as a guest, your comment is unpublished.
    choppa · 1 years ago
    hi is it possible to set it up so that I dont have to press send - it automatically sends the email????
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi,
      Please replace the line .Display with .Send in above VBA code.
  • To post as a guest, your comment is unpublished.
    Corey · 1 years ago
    Is it possbile to write the script so that when you click the button it will attach itself to the email as above but remove the button as well? so that the copy of the file being emailed no longer has the button on it?
    • To post as a guest, your comment is unpublished.
      Danie · 1 years ago
      Just below ".Body = xMailBody" add the following
      .Attachments.Add ActiveWorkbook.FullName
      • To post as a guest, your comment is unpublished.
        Ryno · 1 years ago
        Hi Danie.

        I added the portion as you said, but the email with the attached workbook still have the buttons on.
  • To post as a guest, your comment is unpublished.
    Alex · 1 years ago
    Hi, I am looking to send data from a pivot table data, can you please help