跳到主要內容

如何在復制和粘貼指定範圍到Excel中的電子郵件正文中發送電子郵件?

在許多情況下,Excel工作表中指定範圍的內容可能對您的電子郵件通信很有用。 在本文中,我們將介紹一種直接在Excel中發送將具有指定範圍的電子郵件粘貼到電子郵件正文中的方法。

發送具有指定範圍的電子郵件,粘貼到Excel中的電子郵件正文中
使用出色的工具發送指定範圍的電子郵件,並將其粘貼到電子郵件正文中

有關在Excel中發送郵件的更多教程...


發送具有指定範圍的電子郵件,粘貼到Excel中的電子郵件正文中

以下VBA代碼可以幫助您複製範圍並將其直接粘貼到Excel中的Outlook電子郵件正文中。 請執行以下操作。

1.在工作表中包含您需要復制的範圍,按 其他 + F11 同時打開 Microsoft Visual Basic for Applications 窗口。

2.在開幕 Microsoft Visual Basic for Applications 窗口,請點擊 工具 > 參考 如下圖所示。

3。 在裡面 參考– VBAProject 對話框,請查找並檢查 Microsoft Outlook對像庫 選項,然後單擊 OK 按鈕。

4。 點擊 插入 > 模塊,然後將下面的VBA代碼複製並粘貼到“模塊”窗口中。

VBA代碼:發送具有指定範圍的電子郵件,粘貼到Excel中的電子郵件正文中

Sub Send_Email()
'Updated by Extendoffice 20200119
    Dim xRg As Range
    Dim I, J As Long
    Dim xAddress As String
    Dim xEmailBody As String
    Dim xMailOut As Outlook.MailItem
    Dim xOutApp As Outlook.Application    
    On Error Resume Next
    xAddress = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select range you need to paste into email body", "KuTools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    For I = 1 To xRg.Rows.Count
        For J = 1 To xRg.Columns.Count
            xEmailBody = xEmailBody & "  " & xRg.Cells(I, J).value
        Next
        xEmailBody = xEmailBody & vbNewLine
    Next
    xEmailBody = "Hi" & vbLf & vbLf & " body of message you want to add" & vbLf & vbLf & xEmailBody & vbNewLine
    With xMailOut
        .Subject = "Test"
        .To = ""
        .Body = xEmailBody
        .Display
        '.Send
    End With
    Set xMailOut = Nothing
    Set xOutApp = Nothing
    Application.ScreenUpdating = True
End Sub

筆記:

  • 1)。 請更改電子郵件正文 xEmailBody =“嗨”&vbLf&vbLf&“您要添加的郵件正文”&vbLf&vbLf&xEmailBody&vbNewLine 如你所需。
  • 2)。 請指定您的電子郵件收件人和主題(.到= .Subject =“測試”) 代碼中的行。

5。 按 F5 鍵來運行代碼。 在彈出 Excel的Kutools 對話框,請選擇您需要粘貼到電子郵件正文中的範圍,然後單擊 OK 按鈕。 看截圖:

6.現在,將創建一封包含指定收件人,主題,正文和所選Excel範圍的電子郵件,請單擊 送出 按鈕發送此電子郵件。 請參閱顯示的屏幕截圖。

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


使用出色的工具發送指定範圍的電子郵件,並將其粘貼到電子郵件正文中

如果您不使用Outlook,但仍希望直接在Excel中直接發送電子郵件,並在其中粘貼指定的範圍數據,則強烈建議您使用 發電子郵件 的效用 Excel的Kutools 為了你。 使用此功能,您只需要配置電子郵件地址的傳出服務器,然後以後再通過此電子郵件地址直接在Excel中發送電子郵件。

申請前 Excel的Kutools首先下載並安裝.

1.首先,您需要準備一個帶有必填字段的郵件列表。

  • 保養竅門:郵件列表必須至少包含2行,並且第一行必須為標頭(假設您要將電子郵件發送到Excel中的兩個電子郵件地址,請輸入這兩個電子郵件地址,標頭為“ Email”,如下所示) )。
  • 或者,您可以使用以下命令輕鬆創建郵件列表 創建郵件列表 功能。

2.選擇範圍,將數據添加到電子郵件正文,然後按 按Ctrl + C 鍵複製它。

3.選擇整個郵件列表(包括標題),單擊 Kutools 加 > 發電子郵件。 看截圖:

4.然後 發電子郵件 對話框彈出。

  • 4.1)所選郵件列表的項目填充在相應的字段中(您可以根據需要在郵件列表中添加更多字段);
  • 4.2)點擊電子郵件正文框,然後按 按Ctrl + V 鍵將選定的範圍數據粘貼到其中。 之後,根據需要添加其他內容;
  • 4.3取消選中 通過Outlook發送電子郵件 框;
  • 2.4)點擊 發送服務器設置 按鈕。 看截圖:

5.然後 傳出服務器(SMTP)設置-新方案 對話框彈出。 請在電子郵件地址中填入其服務器設置,並在選中以下內容後指定一個文件夾來保存所有已發送的郵件 將發送的電子郵件保存到 框,然後單擊 OK 按鈕保存設置。

6.返回到 發電子郵件 對話框中,單擊 送出 按鈕發送電子郵件。

從現在開始,您可以直接在Excel中發送具有此功能的電子郵件。

  如果您想免費試用(30天)此實用程序, 請點擊下載,然後按照上述步驟進行操作。


相關文章:

將電子郵件發送到Excel單元格中指定的電子郵件地址
假設您有一個電子郵件地址列表,並且您想直接在Excel中批量向這些電子郵件地址發送電子郵件。 如何實現呢? 本文將向您展示將電子郵件發送到Excel中單元格中指定的多個電子郵件地址的方法。

在Excel中發送電子郵件時插入Outlook簽名
假設要直接在Excel中發送電子郵件,如何在電子郵件中添加默認的Outlook簽名? 本文提供了兩種方法來幫助您在Excel中發送電子郵件時添加Outlook簽名。

發送帶有Excel中附加的多個附件的電子郵件
本文討論的是通過帶有Excel中附加的多個附件的Outlook發送電子郵件。

如果在Excel中已達到到期日期,則發送電子郵件
例如,如果C列中的截止日期小於或等於7天(當前日期為2017/9/13),則向A列中的指定收件人發送電子郵件提醒,並在B列中指定內容。實現這一目標? 本文將提供一種VBA方法來詳細處理它。

根據Excel中的單元格值自動發送電子郵件
假設您要基於Excel中指定的單元格值通過Outlook向特定收件人發送電子郵件。 例如,當工作表中單元格D7的值大於200時,將自動創建一封電子郵件。 本文介紹了一種VBA方法供您快速解決此問題。

有關在Excel中發送郵件的更多教程...

最佳辦公生產力工具

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

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

kte選項卡201905


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

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (22)
Rated 4.5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
The only issue is that the format of the table is not maintained in the mail. Can you let me know how do we preserve the format of the table in the mail, I have the same issue, could you see if you could update it please, otherwise this is excellent.
Many thanks
Rated 4.5 out of 5
This comment was minimized by the moderator on the site
Hi Andy Mitchell,
If you want to maintain the format of the table, the following VBA script can do you a favor. Please give it a try. Thank you.
Sub Send_Email()
'Updated by Extendoffice 20220616
    Dim xRg As Range
    Dim I, J As Long
    Dim xAddress As String
    Dim xMailOut As Object
    Dim xOutApp As Object
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    xAddress = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select range you need to paste into email body", "KuTools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    With xMailOut
        .Subject = "Test"
        .To = ""
        .HTMLBody = RangetoHTML(xRg)
        .Display
        '.Send
    End With
    Set xMailOut = Nothing
    Set xOutApp = Nothing
    Application.ScreenUpdating = True
End Sub

 ' The following VBA script is cited from this page:
 ' https://stackoverflow.com/questions/18663127/paste-excel-range-in-outlook
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
This comment was minimized by the moderator on the site
This worked perfectly, thank you so much.
I can now add my spin on it to get it doing what I need.
This comment was minimized by the moderator on the site
Hi Paul Johnson,

Very happy to help you solve the problem. Have a good day at work.
This comment was minimized by the moderator on the site
hello,
can you help me on below
I have create excel sheet & updated 10 supplier mail detailI have send mail through excel to all 10 supplier with individual sheet attachment with individual mail.
I want to paste excel data in outlook body instead of attachment in mail
can any one help me
This comment was minimized by the moderator on the site
Hi,
Instead of selecting the range, I want to select multiple pivots in the excel.
can you please help me.
This comment was minimized by the moderator on the site
Hi,
Instead of selecting the range manually, I want to select the range automatically.
This comment was minimized by the moderator on the site
Hi Raman,
In the below code, please replace the range "A1:C5" in line Set xRg = Range("A1:C5") with your own range.

Sub Send_Email()
Dim xRg As Range
Dim I, J As Long
Dim xAddress As String
Dim xEmailBody As String
Dim xMailOut As Outlook.MailItem
Dim xOutApp As Outlook.Application
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Range("A1:C5")
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xOutApp = CreateObject("Outlook.Application")
Set xMailOut = xOutApp.CreateItem(olMailItem)
For I = 1 To xRg.Rows.Count
For J = 1 To xRg.Columns.Count
xEmailBody = xEmailBody & " " & xRg.Cells(I, J).Value
Next
xEmailBody = xEmailBody & vbNewLine
Next
xEmailBody = "Hi" & vbLf & vbLf & " body of message you want to add" & vbLf & vbLf & xEmailBody & vbNewLine
With xMailOut
.Subject = "Test"
.To = ""
.Body = xEmailBody
.Display
'.Send
End With
Set xMailOut = Nothing
Set xOutApp = Nothing
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Hi experts, Do we have updates on how the format maintained?
This comment was minimized by the moderator on the site
Hi Ther,
Can't figure it out. Sorry for that.
This comment was minimized by the moderator on the site
i am seeing a compile error (User-defined type not defined". Please help me out to overcome this.
This comment was minimized by the moderator on the site
Hi,
Please get into the Reference window by clicking Tools > references. Scroll down to find and check the Microsoft Outlook Object Library box and click the OK button to finish the setting.
This comment was minimized by the moderator on the site
this is pasting as a text. Kindly suggest how to send the table or the same format which is copied from the excel.
This comment was minimized by the moderator on the site
Code to send automatically after selecting after ok
This comment was minimized by the moderator on the site
Hi
This code is vary excellent, by using the code i have completed my 90% of my project.
I have same issue as mentioned by Anirudh that is table formatting. How can i format the table in email.
Please help me......
This comment was minimized by the moderator on the site
Good Day,
The problem can't be solved yet. Sorry for the inconvenience and thank you for your comment.
This comment was minimized by the moderator on the site
Hi, Is there any update on below.......
This comment was minimized by the moderator on the site
Hi, Also I Wanted code for "Filter by Date".
I am working on project, on that project I wanted to filter the data by the date, actually we have filter/hide the and last 05 days to current date data and we have highlight all other data.
Please help me to complete this project.
Your help is very great-full for me.
This comment was minimized by the moderator on the site
This is great. It is working as expected. The only issue is that the format of the table is not maintained in the mail. Can you let me know how do we preserve the format of the table in the mail
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