跳到主要內容

如何通過Excel從Outlook發送/通過電子郵件發送單元格範圍?

您是否曾經遇到過一個問題,即在完成工作表中的報告後,您需要發送到該工作表中的一系列單元格,這些單元格中包含一些重要數據給特定的收件人。 是否有任何快速方法可以在不打開Outlook的情況下通過Excel通過電子郵件發送此範圍?

使用VBA代碼從Excel發送單元格範圍作為附件

使用VBA代碼從Excel將單元格區域作為主體發送


箭頭藍色右氣泡使用VBA代碼從Excel發送單元格範圍作為附件

以下VBA代碼可以幫助您將所選範圍作為附件發送到Excel中。 請這樣做:

1。 打開您的工作簿,然後按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications窗口.

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:從Excel發送單元格範圍作為附件

Sub SendRange()
'Update 20131209
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim Ws As Worksheet
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim WorkRng As Range
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Wb = Application.ActiveWorkbook
Wb.Worksheets.Add
Set Ws = Application.ActiveSheet
WorkRng.Copy Ws.Cells(1, 1)
Ws.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 = ""
    .CC = ""
    .BCC = ""
    .Subject = "information of kte"
    .Body = "hello, please check and read this document. "
    .Attachments.Add Wb2.FullName
    .Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Ws.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

備註:在上面的代碼中,您可以根據自己的需要更改以下信息。

  • .To = ""
  • .CC =“”
  • .BCC =“”
  • .Subject =“ KTE信息”
  • .Body =“您好,請檢查並閱讀本文檔。”

3。 然後點擊 F5 鍵以運行此代碼,然後會彈出一個提示框,提醒您選擇要發送的範圍。 看截圖:

文檔發送範圍1

4。 然後點擊 OK,進度欄結束後,將出現一個提示框,單擊 ,然後特定範圍的單元格已作為附件發送給您的收件人。

文檔發送範圍2


箭頭藍色右氣泡使用VBA代碼從Excel將單元格區域作為主體發送

如果要從Excel發送特定範圍作為郵件正文的一部分,還可以應用以下VBA代碼來解決它。

Excel的Kutools, 與以上 120 方便的功能,使您的工作更加輕鬆。 

1。 激活您的工作表並按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications窗口.

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:從Excel發送單元格範圍作為正文

Sub EmailRange()
'Update 20131209
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
WorkRng.Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
    .Introduction = "Please read this email."
    .Item.To = ""
    .Item.Subject = "information of kte"
    .Item.Send
End With
Application.ScreenUpdating = True
End Sub

注意: 在上面的代碼中,您可以根據需要更改以下信息。

  • .Introduction =“請閱讀此電子郵件。”
  • .Item.To = ""
  • .Item.Subject =“ KTE信息”

3。 然後點擊 F5 鍵以運行此代碼,然後會彈出一個提示框,提醒您選擇要發送的範圍。

文檔發送範圍1

4. 然後點擊 OK,進度欄結束後,將出現一個提示框,單擊 ,然後特定範圍的單元格已作為郵件正文發送給您的收件人。

文檔發送範圍2

筆記:

1.這些代碼僅在將Outlook作為您的郵件程序時可用。

2.發送完當前工作表後,您可以轉到Outlook以確保電子郵件是否已成功發送。


相關文章:

如何僅通過Excel從Outlook發送工作表?

如何通過Outlook從Excel發送當前工作簿?

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


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

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (27)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi,
Thank you for sharing the code.
How can I re-size the object to make it smaller on the email body?

Thank you so much
This comment was minimized by the moderator on the site
Hi,

I've done everything as above and the result is: all works perfectly apart of the button :) Don't really care about this at the moment :)

Huge thank you!!! topic closed :)
This comment was minimized by the moderator on the site
Thank you for this! I will test it soon as I'll back from holiday. Have one more question. Have an issue with running vba codes by using Activex button (no action at all) even if code works perfectly on edit mode or when run by ctrl+selected button from keyboard. I'm just starting vba journey and have no
idea what I'm doing wrong :/
This comment was minimized by the moderator on the site
Hello, pawerl
You just need to copy and paste the code into the Microsoft Visual Basic for Applications window, and then click the Run button from the tool bar as below screenshot shown: (Note: in the code, S1 is the cell contains the email address that you want to send the email to. Please change them to your need.)

https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-send-emails.png

Please try again!
This comment was minimized by the moderator on the site
Hej,

Mam plik z 6 arkuszami. Każdy z arkuszy 2-6 musi zostać wysłany do innej grupy odbiorców jako załącznik excela. Czy da się to podpiąć pod 5 przycisków?
This comment was minimized by the moderator on the site
Hello, pawerl
To solve your problem, the below article may help you, please check it:

https://www.extendoffice.com/documents/excel/7178-send-each-sheet-to-different-email.html

Thank you!
This comment was minimized by the moderator on the site
Can we fixed ranges ? in macro its self
This comment was minimized by the moderator on the site
Hello everyone
I have a project to select range in sheet 1 to be sent by email one by one to many emails addresses written in sheet 2 in column A and if column B is empty with a certain subject and after sending he write in sheet 2 column b sent
And to wait time 10 seconds to send the next email as the condition of the server
Any one help me please ?   
This comment was minimized by the moderator on the site
Hi, hriad, " if column B is empty with a certain subject and after sending he write in sheet 2 column b sent
And to wait time 10 seconds to send the next email as the condition of the server" I don't understand this. Please retell me about your requirement.
This comment was minimized by the moderator on the site
Hello Sunny
first of all , thanks for your help , as the server put a condition to not send many emails in the same second , I want to wait 10 seconds after sending first email and the following one.
This comment was minimized by the moderator on the site
Hi, hriad, please try below code, after runing it, it will pop out a dialog for selecting a range which you want to send, then it send to addresses in sheet 2 column A and with the subjects in column B. Every 10 seconds, send to one address. You can change the code "Please read this email" as you need.
<div data-tag="code">Sub SendEmailRange()

'UpdateByExtendoffice20220414

Dim WorkRng As Range

Dim xSU, EV As Boolean

Dim xWSh As Worksheet

Dim xCount As Integer

Dim xI As Integer

On Error Resume Next

xTitleId = "KutoolsforExcel"

'select a range that used to send as body

Set WorkRng = Application.InputBox("Range", xTitleId, Application.Selection.Address, , , , , 8)

If WorkRng Is Nothing Then Exit Sub
WorkRng.Activate
WorkRng.Select

Set xWSh = ActiveWorkbook.Worksheets("Sheet2") 'the sheet that contains addresses and subjects

xCount = xWSh.UsedRange.Rows.Count

xSU = Application.ScreenUpdating

EV = ActiveWorkbook.EnvelopeVisible

Application.ScreenUpdating = False


For xI = 1 To xCount

If (xWSh.Range("A" & xI) = "") Then
Exit For
End If
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Introduction = "Please read this email."

.Item.To = xWSh.Range("A" & xI)

.Item.Subject = xWSh.Range("B" & xI)

.Item.Send
End With
If (xI = xCount) Then

Exit For

End If

Application.Wait Now + TimeValue("0:00:10")

Next


Application.ScreenUpdating = xSU

ActiveWorkbook.EnvelopeVisible = EV

End Sub

This comment was minimized by the moderator on the site
Hello Sunny
Many many Thanks for your effort.... Thanks a lot
This comment was minimized by the moderator on the site
Hello everyone
I have a project to select range in sheet 1 to be sent by email one by one to many emails addresses written in sheet 2 in column A and if column B is empty with a certain subject and after sending he write in sheet 2 column b sent
And to wait time 10 seconds to send the next email as the condition of the server
Any one help me please ?   
This comment was minimized by the moderator on the site
Hi I have a table set up that I want staff to fill in and then a command button that will then automatically generate an email, however I want the data from the table to be in the email body. Is there a code for that? 
Thank you 
This comment was minimized by the moderator on the site
ich möchte das kopierte nicht als Datei in eine E-Mail einfügen, sondern per copy and paste in die E-Mail einfügen. Wie geht das in dem VBA-Code?
This comment was minimized by the moderator on the site
could we put as many email addresses to send to?
This comment was minimized by the moderator on the site
Hi, Carey, all above the VBAs can add many emails address (To, BCC, CC) to send to by using ; as separator. For instance,  .To = "; ; "
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