Note: The other languages of the website are Google-translated. Back to English
Logga in  \/ 
x
or
x
Registrera  \/ 
x

or

Hur skickar jag ett e-postmeddelande via Outlook när arbetsboken sparas i Excel?

Den här artikeln talar om att skicka ett e-postmeddelande via Outlook när en specifik arbetsbok sparas i Excel. Gör som självstudien visar.

Skicka ett e-postmeddelande via Outlook när arbetsboken sparas med VBA-kod


Skicka ett e-postmeddelande via Outlook när arbetsboken sparas med VBA-kod


Gör så här för att skicka ett e-postmeddelande via Outlook när arbetsboken sparas i Excel.

1. Spara arbetsboken först som en Excel-makroaktiverad arbetsbok. Klick Fil > Spara som. I Spara som Välj en mapp för att spara arbetsboken, namnge den i rutan Filnamn, välj Excel Macro-Enabled Workbook från Spara som rullgardinsmenyn och klicka sedan på Save knapp. Se skärmdump:

2. Öppna Excel Macro-aktiverad arbetsbok som du har sparat just nu, tryck på andra + F11 samtidigt för att öppna Microsoft Visual Basic för applikationer fönster.

2. I Microsoft Visual Basic för applikationer dubbelklicka Denna arbetsbok i det vänstra fältet och kopiera sedan och klistra in nedanstående VBA-kod i Denna arbetsbok kodfönster. Se skärmdump:

VBA-kod: Skicka e-post när arbetsboken sparas

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
'Updated by Extendoffice 20181102
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xName As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)
    xName = ActiveWorkbook.FullName
    With xMailItem
        .To = "Email Address"
        .CC = ""
        .Subject = "The workbook has been saved"
        .Body = "Hi," & Chr(13) & Chr(13) & "File is now updated."
        .Attachments.Add xName
        .Display
       '.send
    End With
    Set xMailItem = Nothing
    Set xOutApp = Nothing
End Sub

Anmärkningar: Byt ut E-postadress med mottagarens e-postadress i raden .To = "E-postadress"och ändra Cc, ämne såväl som kroppsfält i VBA-koden som du behöver.

3. tryck på andra + Q samtidigt för att stänga Microsoft Visual Basic för applikationer fönster.

Från och med nu, när du uppdaterar arbetsboken och sparar den, skapas ett e-postmeddelande automatiskt med uppdaterad arbetsbok bifogad. Klicka på Skicka knappen för att skicka e-postmeddelandet. Se skärmdump:

Anmärkningar: VBA-koden fungerar bara när du använder Outlook som ditt e-postprogram.


Relaterade artiklar:


De bästa Office-produktivitetsverktygen

Kutools för Excel löser de flesta av dina problem och ökar din produktivitet med 80%

  • återanvändning: Sätt snabbt i komplexa formler, diagram och allt som du har använt tidigare; Kryptera celler med lösenord; Skapa e-postlista och skicka e-post ...
  • Super Formula Bar (enkelt redigera flera rader med text och formel); Läslayout (enkelt läsa och redigera ett stort antal celler); Klistra in i filtrerat intervall...
  • Sammanfoga celler / rader / kolumner utan att förlora data; Delat cellinnehåll; Kombinera duplicerade rader / kolumner... Förhindra duplicerade celler; Jämför intervall...
  • Välj Duplicera eller Unikt Rader; Välj tomma rader (alla celler är tomma); Super Find och Fuzzy Find i många arbetsböcker; Slumpmässigt val ...
  • Exakt kopia Flera celler utan att ändra formelreferens; Skapa referenser automatiskt till flera ark; Sätt in kulor, Kryssrutor och mer ...
  • Extrahera text, Lägg till text, ta bort efter position, Ta bort mellanslag; Skapa och skriva ut personsökningstalsatser; Konvertera mellan celler innehåll och kommentarer...
  • Superfilter (spara och tillämpa filterscheman på andra ark); Avancerad sortering efter månad / vecka / dag, frekvens och mer; Specialfilter av fet, kursiv ...
  • Kombinera arbetsböcker och arbetsblad; Sammanfoga tabeller baserat på nyckelkolumner; Dela data i flera ark; Batchkonvertera xls, xlsx och PDF...
  • Mer än 300 kraftfulla funktioner. Stöder Office / Excel 2007-2019 och 365. Stöder alla språk. Enkel distribution i ditt företag eller organisation. Fullständiga funktioner 30-dagars gratis provperiod. 60-dagars pengarna tillbaka-garanti.
kte-flik 201905

Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50%och minskar hundratals musklick för dig varje dag!
officetab botten
Say something here...
symbols left.
You are guest
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.
    florin · 1 years ago
    Dear All, can some one help me, I'm a novice in VBA coding, I have made some modification, but how can I do so send email if the workbook is saved, and if user name is different, like if user name of the station is glade2 then send email if the workbook is saved, else do not send.

    Thank you so much for your support
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi florin,
      Didn't get your point. What does your username represent?
      • To post as a guest, your comment is unpublished.
        Florin Dan · 1 years ago
        Hi crystal, thank you for your reply, usernane is enviroment usernane, and i have done it, i did use a if function and exit sub.
        Thank you so much.
  • To post as a guest, your comment is unpublished.
    Brent · 1 years ago
    Hello - How can I include cell data in the email "cc" field?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Brent,
      Supposing you want to include the value in cell a7 in the email "cc" field, please try the below VBA.

      Private Sub Workbook_AfterSave(ByVal Success As Boolean)
      'Updated by Extendoffice 20200628
      Dim xOutApp As Object
      Dim xMailItem As Object
      Dim xName As String
      On Error Resume Next
      Set xOutApp = CreateObject("Outlook.Application")
      Set xMailItem = xOutApp.CreateItem(0)
      xName = ActiveWorkbook.FullName
      With xMailItem
      .To = "Email Address"
      .CC = Range("a7").Value
      .Subject = "The workbook has been saved"
      .Body = "Hi," & Chr(13) & Chr(13) & "File is now updated."
      .Attachments.Add xName
      .Display
      '.send
      End With
      Set xMailItem = Nothing
      Set xOutApp = Nothing
      End Sub
  • To post as a guest, your comment is unpublished.
    Lena · 1 years ago
    Hi! Thanks a lot for this guideline :-) I would like to do something more in this code - send an email based on the countries. It means that I have to create commands with if and select. Right? I have already delete the attachment from email. I would like to add the link with path into folder instead. But when macro run, command is not valid :-(
    I appreciate each help how to add it there.
  • To post as a guest, your comment is unpublished.
    Mike · 2 years ago
    Is there a way to have the automated email be auto encrypted?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Mike,
      Sorry can't help to solve this problem. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    mayle4pam · 2 years ago
    How would this be handled for an Office 365 document. It is automatically saving.
  • To post as a guest, your comment is unpublished.
    Chris · 3 years ago
    Hi, nice article! One thing ive been trying to achieve with this is to attach the current state of the workbook to the email.

    At the moment, it only sends the original state of the file and doesnt include any changes the user would have made.

    Any ideas on how to implement this using a macro?
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Hi Chris,
      The code has been updated with the problem solved, please have a try. Thank you for your comment.
      • To post as a guest, your comment is unpublished.
        Tin Hwee Lee · 2 years ago
        Hi Chris,

        I have chanced upon the same issue.
        Currently the codes are used in the "beforesave" module.
        Which means that the email will send the spreadsheet that is before saved.

        There is another module "aftersave".
        I applied the code in this module and it worked like a charm.
  • To post as a guest, your comment is unpublished.
    Bob · 3 years ago
    Dear all, I would like to ask how to attach functional link to certain folder on server, if I paste the link, it appears in the workbook just like plain text and so it doensn't work in received e-mail, how can I turn it into link, so recepiants can click on it?
    I would like to use this way instead sending enclosed excel file.
    Thanks for advice
    • To post as a guest, your comment is unpublished.
      Grant · 2 years ago
      I put this in the body of the email and it worked for me...
      "file:///Z:\dir1\dir2\dir3\Test1.xlsm"
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Hi Robert,
      Sorry can’t help with this, welcome to post any question about Excel to our forum: https://www.extendoffice.com/forum.html. You will get more Excel supports from our professional or other Excel fans.
  • To post as a guest, your comment is unpublished.
    Antonio · 3 years ago
    Gracias. Consulta: utilizando esta misma rutina, como podría enviar el correo a un correo específico dependiendo el valor de otra celda?
  • To post as a guest, your comment is unpublished.
    Jermaine · 3 years ago
    How to automate the email notification in VBA based on Date range, without having to see the pop up for security permission to allow VBA to send the email.