Μετάβαση στο κύριο περιεχόμενο

Πώς να στείλετε email εάν κάνετε κλικ στο κουμπί στο Excel;

Ας υποθέσουμε ότι πρέπει να στείλετε email μέσω του Outlook κάνοντας κλικ σε ένα κουμπί στο φύλλο εργασίας του Excel, πώς μπορείτε να το κάνετε; Αυτό το άρθρο θα εισαγάγει μια μέθοδο VBA για να την επιτύχει λεπτομερώς.

Στείλτε email εάν κάνετε κλικ στο κουμπί με τον κωδικό VBA


Στείλτε email εάν κάνετε κλικ στο κουμπί με τον κωδικό VBA

Κάντε τα εξής για να στείλετε ένα email μέσω του Outlook εάν κάνετε κλικ σε ένα κουμπί εντολών στο βιβλίο εργασίας του Excel.

1. Εισαγάγετε ένα κουμπί εντολής στο φύλλο εργασίας σας κάνοντας κλικ Εργολάβος > Κύριο θέμα > Κουμπί εντολών (ActiveX Control). Δείτε το στιγμιότυπο οθόνης:

2. Κάντε δεξί κλικ στο εισαγόμενο κουμπί εντολών και, στη συνέχεια, κάντε κλικ στο Προβολή κωδικού από το μενού με δεξί κλικ όπως φαίνεται παρακάτω.

3. Στο άνοιγμα Microsoft Visual Basic για εφαρμογές παράθυρο, αντικαταστήστε τον αρχικό κωδικό στο παράθυρο Κωδικός με το ακόλουθο σενάριο VBA.

Κωδικός VBA: Στείλτε email εάν κάνετε κλικ στο κουμπί στο 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). Αλλάξτε το σώμα του email όπως χρειάζεστε στο xMailBody γραμμή στον κώδικα.

2). Αντικαταστήστε το Διεύθυνση ηλεκτρονικού ταχυδρομείου με τη διεύθυνση email του παραλήπτη στη γραμμή .To = "Διεύθυνση ηλεκτρονικού ταχυδρομείου".

3). Καθορίστε τους παραλήπτες Cc και Bcc όπως χρειάζεστε .CC = "" και .Bcc = "" ενότητες.

4). Αλλάξτε το θέμα του email στη σειρά .Subject = "Δοκιμή αποστολής email με κλικ στο κουμπί".

4. Πάτα το άλλος + Q ταυτόχρονα για να κλείσετε το Microsoft Visual Basic για εφαρμογές παράθυρο.

5. Απενεργοποιήστε τη λειτουργία σχεδίασης κάνοντας κλικ Εργολάβος > Λειτουργία σχεδίασης. Δείτε το στιγμιότυπο οθόνης:

Από τώρα και στο εξής, κάθε φορά που κάνετε κλικ στο κουμπί εντολών, ένα email θα δημιουργείται αυτόματα με καθορισμένους παραλήπτες, θέμα και σώμα. Στείλτε το email κάνοντας κλικ στο Αποστολή κουμπί.

Note: Ο κωδικός VBA λειτουργεί μόνο όταν χρησιμοποιείτε το Outlook ως πρόγραμμα ηλεκτρονικού ταχυδρομείου.

Στείλτε εύκολα email μέσω του Outlook με βάση τα πεδία της δημιουργημένης λίστας αλληλογραφίας στο Excel:

Η Αποστολή email χρησιμότητα του Kutools για Excel βοηθά στην αποστολή email μέσω του Outlook με βάση τα πεδία της δημιουργημένης λίστας αλληλογραφίας στο Excel.
Κατεβάστε το και δοκιμάστε το τώρα! (Δωρεάν διαδρομή 30 ημερών)


Σχετικά άρθρα:

Τα καλύτερα εργαλεία παραγωγικότητας γραφείου

🤖 Kutools AI Aide: Επανάσταση στην ανάλυση δεδομένων με βάση: Ευφυής Εκτέλεση   |  Δημιουργία κώδικα  |  Δημιουργία προσαρμοσμένων τύπων  |  Αναλύστε δεδομένα και δημιουργήστε γραφήματα  |  Επίκληση Λειτουργιών Kutools...
Δημοφιλή χαρακτηριστικά: Εύρεση, επισήμανση ή αναγνώριση διπλότυπων   |  Διαγραφή κενών γραμμών   |  Συνδυάστε στήλες ή κελιά χωρίς απώλεια δεδομένων   |   Γύρος χωρίς φόρμουλα ...
Σούπερ Αναζήτηση: VLookup πολλαπλών κριτηρίων    VLookup πολλαπλών τιμών  |   VLookup σε πολλά φύλλα   |   Ασαφής αναζήτηση ....
Σύνθετη αναπτυσσόμενη λίστα: Γρήγορη δημιουργία αναπτυσσόμενης λίστας   |  Εξαρτημένη αναπτυσσόμενη λίστα   |  Πολλαπλή αναπτυσσόμενη λίστα ....
Διαχειριστής στήλης: Προσθέστε έναν συγκεκριμένο αριθμό στηλών  |  Μετακίνηση στηλών  |  Εναλλαγή κατάστασης ορατότητας κρυφών στηλών  |  Συγκρίνετε εύρη και στήλες ...
Επιλεγμένα Χαρακτηριστικά: Εστίαση πλέγματος   |  Προβολή σχεδίου   |   Μεγάλη Formula Bar    Διαχείριση βιβλίου εργασίας & φύλλου   |  Βιβλιοθήκη πόρων (Αυτόματο κείμενο)   |  Επιλογή ημερομηνίας   |  Συνδυάστε φύλλα εργασίας   |  Κρυπτογράφηση/Αποκρυπτογράφηση κελιών    Αποστολή email ανά λίστα   |  Σούπερ φίλτρο   |   Ειδικό φίλτρο (φίλτρο με έντονη γραφή/πλάγια γραφή/διαγραφή...) ...
Κορυφαία 15 σύνολα εργαλείων12 Κείμενο Εργαλεία (Προσθήκη κειμένου, Κατάργηση χαρακτήρων, ...)   |   50 + Διάγραμμα Τύποι (Gantt διάγραμμα, ...)   |   40+ Πρακτικό ΜΑΘΗΜΑΤΙΚΟΙ τυποι (Υπολογίστε την ηλικία με βάση τα γενέθλια, ...)   |   19 Εισαγωγή Εργαλεία (Εισαγωγή κωδικού QR, Εισαγωγή εικόνας από το μονοπάτι, ...)   |   12 Μετατροπή Εργαλεία (Αριθμοί σε λέξεις, Μετατροπή Συναλλάγματος, ...)   |   7 Συγχώνευση & διαχωρισμός Εργαλεία (Σύνθετες σειρές συνδυασμού, Διαίρεση κελιών, ...)   |   ... κι αλλα

Αυξήστε τις δεξιότητές σας στο Excel με τα Kutools για Excel και απολαύστε την αποτελεσματικότητα όπως ποτέ πριν. Το Kutools για Excel προσφέρει πάνω από 300 προηγμένες δυνατότητες για την ενίσχυση της παραγωγικότητας και την εξοικονόμηση χρόνου.  Κάντε κλικ εδώ για να αποκτήσετε τη δυνατότητα που χρειάζεστε περισσότερο...

Περιγραφή


Το Office Tab φέρνει τη διεπαφή με καρτέλες στο Office και κάνει την εργασία σας πολύ πιο εύκολη

  • Ενεργοποίηση επεξεργασίας και ανάγνωσης καρτελών σε Word, Excel, PowerPoint, Publisher, Access, Visio και Project.
  • Ανοίξτε και δημιουργήστε πολλά έγγραφα σε νέες καρτέλες του ίδιου παραθύρου και όχι σε νέα παράθυρα.
  • Αυξάνει την παραγωγικότητά σας κατά 50% και μειώνει εκατοντάδες κλικ του ποντικιού για εσάς κάθε μέρα!
Comments (76)
Rated 3.5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Hi,

How do I send only one sheet from my workbook and not the whole workbook with the submitting button?
This comment was minimized by the moderator on the site
Hi Diana,
Please try if the following VBA code can help.
Before using this code, make sure to replace ThisWorkbook.Sheets("Sheet1") with the actual name of the worksheet you want to attach. Also, be sure to set the email’s .To property to the actual recipient’s address.

This code creates a temporary workbook file in your temp folder, attaches it to an email, and then shows this email. After showing the email, it will delete the temporary file. If you prefer to send the email directly without displaying it, you can replace the .Display method with .Send.

Private Sub CommandButton1_Click()
'Updated by Extendoffice 20240411
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Dim xSheet As Worksheet
    Dim xTempWorkbook As Workbook
    Dim xTempFilePath As String
    Dim xTempFileName As String

    On Error GoTo ErrHandler
    ' Set the name of the worksheet you want to attach (e.g., "Sheet1")
    Set xSheet = ThisWorkbook.Sheets("Sheet1")

    ' Create the path and filename for the temporary file
    xTempFilePath = Environ$("TEMP") & "\"
    xTempFileName = "TempWorkbook" & Format(Now, "yyyymmddhhmmss") & ".xlsx"

    ' Copy the worksheet to a new workbook
    xSheet.Copy
    Set xTempWorkbook = ActiveWorkbook

    ' Save the new workbook as a temporary file
    xTempWorkbook.SaveAs xTempFilePath & xTempFileName

    ' Close the new workbook
    xTempWorkbook.Close SaveChanges:=False

    ' Create the Outlook application and Mail item
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)

    ' Set the email body content
    xMailBody = "Body content" & vbNewLine & vbNewLine & _
                "This is line 1" & vbNewLine & _
                "This is line 2"

    ' Set up the email details and display the email
    With xOutMail
        .To = "" ' Set the recipient's email address
        .CC = ""
        .BCC = ""
        .Subject = "Test email sent by button clicking"
        .Body = xMailBody
        .Attachments.Add xTempFilePath & xTempFileName ' Attach the file
        .Display ' or use .Send to send the email directly
    End With

    Kill xTempFilePath & xTempFileName

    Set xOutMail = Nothing
    Set xOutApp = Nothing
    Set xSheet = Nothing
    Exit Sub

ErrHandler:
    MsgBox "An error has occurred: " & Err.Description
End Sub
This comment was minimized by the moderator on the site
Why is it that the filename of the attachment has the %20 filled in for the spaces? How to remove them and have the original file name, Price Discrepancy form.xlsm instead of Price%20Discrepancy%20form.xlsm?
Thank you.
This comment was minimized by the moderator on the site
Hi There,

I want to be able to attach a spreadsheet to an email and send it off, however, the difference is in the spreadsheet there is a date in cell A1 and description of works in cell A3, I want to be able to combine those and rename the spreadsheet to the attachments as per the date and description of works.

Thanks
This comment was minimized by the moderator on the site
Hi Fadi,
The following VBA code can do you a favor. Please give it a try. Thank you.
Private Sub CommandButton1_Click()

    'Update 20221123
    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
    Application.DisplayAlerts = True
    
    FileName = Format(ActiveSheet.Range("A1").Value, "dd-mmm-yy") & " " & ActiveSheet.Range("A3").Value
    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") & "\"

    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    Debug.Print FilePath & FileName & xFile
    Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
    With OutlookMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Type your subject here"
        .Body = "Type your email body here."
        .Attachments.Add Wb2.FullName
        .Display
'        .Send
    End With
    Wb2.Close
    Kill FilePath & FileName & xFile
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Application.ScreenUpdating = True
    Application.DisplayAlerts = False

End Sub
This comment was minimized by the moderator on the site
Hello Guys,

Could you please help me with a VB code which should expand, Ungroup or Unhide base on if cell is selected with X and Y value
This comment was minimized by the moderator on the site
Hi Santosh,
I don't quite understand what you mean. You may need to attach a screenshot or a sample file to describe the problem you encountered more clearly.
This comment was minimized by the moderator on the site
Hi,
In my excel there is an chart, is there a way that when the button is pressed, the email is generated with the chart included into the body of the email?
Rated 3.5 out of 5
This comment was minimized by the moderator on the site
Hi Jack,
The following VBA code can do you a favor. After clicking the button, a dialog box will pop up, please enter the name of the chart you will include in your email body.
In the code, please change "Sheet1" to the name of the sheet that contains the chart you will send.
Private Sub CommandButton1_Click()
'Updated by Extendoffice 20220826
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xStartMsg As String
    Dim xEndMsg As String
    Dim xChartName As String
    Dim xChartPath As String
    Dim xPath As String
    Dim xChart As ChartObject
    On Error Resume Next
    xChartName = Application.InputBox("Please enter the chart name:", "KuTools for Excel", , , , , , 2)
    If xChartName = "" Then Exit Sub
    Set xChart = Sheets("Sheet1").ChartObjects(xChartName) 'Change "Sheet1" to your worksheet name
    If xChart Is Nothing Then Exit Sub
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xStartMsg = "<font size='5' color='black'> Good Day," & "<br> <br>" & "Please find the chart below: " & "<br> <br> </font>"
    xEndMsg = "<font size='4' color='black'> Many Thanks," & "<br> <br> </font>"
    xChartPath = Application.ActiveWorkbook.Path & "\" & Environ("USERNAME") & VBA.Format(VBA.Now(), "DD_MM_YY_HH_MM_SS") & ".bmp"
    xPath = "<p align='Left'><img src="/%20&%20"cid:" & Mid(xChartPath, InStrRev(xChartPath, "\") + 1) & """  width=700 height=500 > <br> <br>"
    xChart.Chart.Export xChartPath
    With xOutMail
        .To = ""
        .Subject = "Add Chart in outlook mail body"
        .Attachments.Add xChartPath
        .HTMLBody = xStartMsg & xPath & xEndMsg
        .Display
    End With
    Kill xChartPath
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
This comment was minimized by the moderator on the site
Hi

I'm trying the initial request to simply have a button to open a new email but it doesn't seem to work.

Wondering if it has something to do with the " 'Updated by Extendoffice 2017/9/14" date.

Please let me know how to update this so I can get the button working :)
This comment was minimized by the moderator on the site
Hi Jonathan Matthias,
This line 'Updated by Extendoffice 2017/9/14" is a remark we give to the VBA code, which has nothing to do with the running of the VBA code.
Please make sure that CommandButton1 in the first line of the code is the same name as your button.
The name of the button will be displaysed in the Name Box after selecting it. See the screenshot below.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/email_button.png
This comment was minimized by the moderator on the site
Hi everyone,

On the lines of the email I'm trying right 4 lines of text and even adding "vbNewLine" is returning some errors. Also I'm trying to reference a column on the email subject and isn't showing anything. I really would appreciate any help.

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 = "Hi Kaitlyn," & vbNewLine & vbNewLine & _
"Please see the attached NPI form for for you review and approval." & vbNewLine & vbNewLine _
"Many thanks in advance, Liz"

On Error Resume Next
With xOutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Updated NPI Form" & (B5)
.Body = xMailBody
.Attachments.Add ActiveWorkbook.FullName
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Sub GroupBox542_Click()
End Sub
This comment was minimized by the moderator on the site
Hi Camila,
The following VBA code can do you a favor. Please give it a try. Thank you.
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 = "Hi Kaitlyn," & vbNewLine & _
               "The second line" & vbNewLine & _
              "Please see the attached NPI form for for you review and approval." & vbNewLine & _
              "Many thanks in advance, Liz"
                  On Error Resume Next
    With xOutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Updated NPI Form" & Range("B5")
        .Body = xMailBody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
This comment was minimized by the moderator on the site
How do I add code so that when the user submits the form via email to prompt them to include their name
This comment was minimized by the moderator on the site
Hi Susy Fong,
I don't quite understand what you mean. Can you explain it more specifically?
This comment was minimized by the moderator on the site
Hi, your tutorial has been very useful but if I wanted to include a range in the mail body instead of a string how would I go about that. Currently replacing the strings by referencing the cells is not working eg. xMailBody = ThisWorkbook.Activeworksheet("sheet1").Range("A2:F40") does not work
This comment was minimized by the moderator on the site
Activeworksheet("sheet1").Range("A2:F40").Value will work
This comment was minimized by the moderator on the site
Hi, perfect. Thank you. Is there any possibility to set also from which mail adress should be the mail sent? (in Outlook, I have two adresses, it automatically set one adress, but I need the second just for this makro) Thanks
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations