Tip: andere talen zijn Google-Vertaald. Je kunt het English versie van deze link.
Log in
x
or
x
x
Registreren
x

or

Hoe e-mail verzenden als op de knop wordt geklikt in Excel?

Stel dat u e-mail moet verzenden via Outlook door op een knop in het Excel-werkblad te klikken, hoe kunt u dat dan doen? Dit artikel introduceert een VBA-methode om dit in details te bereiken.

Stuur een e-mail als op de knop wordt geklikt met VBA-code


Verstuur eenvoudig e-mail via Outlook op basis van gemaakte mailinglijst:

De Stuur emails utility van Kutools voor Excel helpt gebruikers bij het verzenden van e-mail via Outlook op basis van gemaakte mailinglijst in Excel. Zie screenshot:

Kutools for Excel: met meer dan 200 handige Excel-add-ins, gratis om zonder beperking in 60-dagen te proberen. Download en gratis proef nu!

Tabblad Office Schakel bewerken en browsen met tabbladen in Office in en maak uw werk veel eenvoudiger ...
Kutools voor Excel brengt 300 geavanceerde functies naar Excel en verhoogt uw productiviteit met 80%
  • Super Formula Bar (bewerk eenvoudig meerdere regels tekst en formule); Leesindeling (gemakkelijk grote aantallen cellen lezen en bewerken); Plakken in gefilterd bereik ...
  • Cellen / rijen / kolommen samenvoegen en gegevens bewaren; Inhoud gesplitste cellen; Combineer dubbele rijen en som / gemiddelde ... Voorkom dubbele cellen; Bereiken vergelijken ...
  • Selecteer Dubbele of Unieke rijen; Selecteer Lege rijen (alle cellen zijn leeg); Super Find en Fuzzy Find in veel werkmappen; Willekeurig selecteren ...
  • Kopieer exact meerdere cellen zonder de formuleverwijzing te wijzigen; Automatisch verwijzingen maken naar meerdere bladen; Kogels invoegen, selectievakjes en meer ...
  • Favoriete en snel formules, reeksen, grafieken en afbeeldingen invoegen; Versleutel cellen met wachtwoord; Maak een mailinglijst en verzend e-mails ...
  • Tekst extraheren, tekst toevoegen, verwijderen op positie, spatie verwijderen; Subtotalen voor paging maken en afdrukken; Converteren tussen cellen Inhoud en opmerkingen ...
  • Super Filter (bewaar en pas filterschema's toe op andere bladen); Geavanceerd Sorteren op maand / week / dag, frequentie en meer; Speciaal filter op vet, cursief ...
  • Werkboeken en werkbladen combineren; Tabellen samenvoegen op basis van sleutelkolommen; Gegevens splitsen in meerdere bladen; Batch Convert xls, xlsx en PDF ...
  • Werkt met Office 2007-2019 en 365 en ondersteunt alle talen. Het is eenvoudig te implementeren in uw bedrijf. Volledige functionaliteit 60-daagse gratis proefversie.

Stuur een e-mail als op de knop wordt geklikt met VBA-code


Ga als volgt te werk om via Outlook een e-mail te verzenden als op een opdrachtknop wordt geklikt in Excel-werkmap.

1. Voeg een opdrachtknop in uw werkblad in door op te klikken ontwikkelaar > bijvoegsel > Commandoknop (ActiveX-besturingselement). Zie screenshot:

2. Klik met de rechtermuisknop op de ingevoegde opdrachtknop en klik vervolgens op Bekijk code vanuit het rechtsklikmenu, zoals hieronder te zien is.

3. In de opening Microsoft Visual Basic voor toepassingen venster, vervang de originele code in het codevenster door het volgende VBA-script.

VBA-code: stuur een e-mail als op de knop wordt geklikt in 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

Notes:

1). Wijzig de e-mailtekst zoals u nodig hebt in de xMailBody regel in de code.

2). Vervang de E-mail adres met het e-mailadres van de ontvanger in lijn .To = "E-mailadres".

3). Geef de Cc- en Bcc-ontvangers op zoals u nodig hebt .CC = "" en .Bcc = "" secties.

4). Verander het onderwerp van de e-mail in de rij .Subject = "Test e-mail verzonden door klikken op knop".

4. druk de anders + Q toetsen tegelijkertijd om de Microsoft Visual Basic voor toepassingen venster.

5. Schakel de ontwerpmodus uit door op te klikken ontwikkelaar > Ontwerpmodus. Zie screenshot:

Vanaf nu, elke keer dat u op de Command-knop klikt, wordt er automatisch een e-mail aangemaakt met opgegeven ontvangers, onderwerp en hoofdtekst. Stuur de e-mail door op de Verstuur knop.

Notes: De VBA-code werkt alleen als u Outlook als uw e-mailprogramma gebruikt.


Gerelateerde artikelen:


  • Super Formula Bar (bewerk eenvoudig meerdere regels tekst en formule); Lay-out lezen (gemakkelijk grote aantallen cellen lezen en bewerken); Plakken op gefilterd bereik...
  • Cellen / rijen / kolommen samenvoegen en gegevens bewaren; Inhoud gesplitste cellen; Combineer dubbele rijen en som / gemiddelde... voorkomen dubbele cellen; Ranges vergelijken...
  • Selecteer Dupliceren of Uniek rijen; Selecteer Lege rijen (alle cellen zijn leeg); Super Find en Fuzzy Find in veel werkboeken; Willekeurig selecteren ...
  • Exacte kopie Meerdere cellen zonder formule-referentie te wijzigen; Automatisch referenties maken naar meerdere vellen; Voeg kogels toe, Selectievakjes en meer ...
  • Favoriete en snel formules invoegen, Bereiken, grafieken en afbeeldingen; Coderen van cellen met wachtwoord; Maak een mailinglijst en stuur e-mails ...
  • extract Text, Tekst toevoegen, verwijderen op positie, Verwijder de spatie; Subtotalen voor paging maken en afdrukken; Converteren tussen cellen Inhoud en opmerkingen...
  • Super filter (bewaar en pas filterschema's toe op andere bladen); Geavanceerde sortering per maand / week / dag, frequentie en meer; Speciaal filter door vet, cursief ...
  • Combineer werkmappen en werkbladen; Tabellen samenvoegen op basis van sleutelkolommen; Gegevens splitsen in meerdere bladen; Batch Converteer xls, xlsx en PDF...
  • Werkt met Office 2007-2019 en 365 en ondersteunt alle talen. Het is eenvoudig te implementeren in uw bedrijf. Volledige functionaliteit 60-daagse gratis proefversie.
kte-tab 201905
  • Bewerken en lezen met tabbladen inschakelen in Word, Excel, PowerPoint, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster, in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en verlaagt dagelijks honderden muisklikken voor u!
Officetab onderaan
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.
    Courtney · 6 days 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 · 15 days 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 · 1 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 · 2 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 · 2 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 · 1 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 · 2 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 · 1 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. · 2 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 · 1 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 · 3 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 · 1 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 · 5 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 · 6 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 · 6 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 · 6 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 · 6 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 · 7 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 · 6 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 · 8 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 · 7 months ago
      Hi,
      You need to assign different codes to each button.
  • To post as a guest, your comment is unpublished.
    erick · 11 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"

    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 · 11 months 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 · 11 months 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