Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

當工作簿保存在Excel中時,如何​​通過Outlook發送電子郵件?

本文討論的是在Excel中保存特定工作簿時通過Outlook發送電子郵件。 請按照本教程所示進行操作。

使用VBA代碼保存工作簿時,通過Outlook發送電子郵件


使用VBA代碼保存工作簿時,通過Outlook發送電子郵件


若要在工作簿保存在Excel中時通過Outlook發送電子郵件,請執行以下操作。

1.請首先將工作簿另存為Excel Macro-Enabled Workbook。 請點擊 文件 > 另存為。 在 另存為 對話框中,選擇一個文件夾來保存工作簿,在“文件名”框中將其命名,然後選擇 Excel啟用宏的工作簿 來自 保存類型 下拉列表,然後單擊 慳了 按鈕。 看截圖:

2.打開剛才保存的Excel Macro-Enabled Workbook,按 其他 + F11 同時打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口,請雙擊 的ThisWorkbook 在左側欄中,然後將以下VBA代碼複製並粘貼到 的ThisWorkbook 代碼窗口。 看截圖:

VBA代碼:保存工作簿時發送電子郵件

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

備註:請更換 電郵地址 收件人電子郵件地址在一行 .To =“電子郵件地址”根據需要在VBA代碼中更改抄送,主題以及正文字段。

3。 按 其他 + Q 同時關閉按鍵 Microsoft Visual Basic for Applications 窗口。

從現在開始,當您更新並保存工作簿時,將自動創建一封電子郵件,其中附有更新的工作簿。 請點擊 發送 按鈕發送電子郵件。 看截圖:

備註:僅當您將Outlook用作電子郵件程序時,VBA代碼才起作用。


相關文章:


最佳辦公效率工具

Kutools for Excel 解決了您的大部分問題,並將您的生產力提高了 80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 300 多項強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
officetab底部
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 · 2 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.