【问题标题】:How to add Excel ranges and text to email body, in specific positions?如何在特定位置将 Excel 范围和文本添加到电子邮件正文?
【发布时间】:2020-07-27 11:06:10
【问题描述】:

我想将 Excel 工作表中多个范围的数据复制到电子邮件正文。

下面是我想出的代码。

如何使范围在另一个之下粘贴,以及如何在范围之后但在 Outlook 签名之前添加文本。

现在怎么样了:

Sub reportCostLunch()

Dim recipient(0) As Variant
recipient(0) = ""
Dim outlook As Object
Dim email As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim lastRow As Long
Dim sheet, sheet1 As Worksheet
Dim SDest As String, title As String, slot As String

Set sheet = ThisWorkbook.Sheets("SHEET1")
Set sheet1 = ThisWorkbook.Sheets("SHEET2")
title = sheet.Range("D13").Value
Set outlook = CreateObject("Outlook.Application")
Set email = outlook.CreateItem(0)

With email
    SDest = ""
    For i = LBound(recipient) To UBound(recipient)
        If SDest = "" Then
            SDest = recipient(i)
        Else
            SDest = SDest & ";" & recipient(i)
        End If
    Next i
    
    .To = SDest
    .Subject = title
    .Display

    Set xInspect = email.GetInspector
    Set pageEditor = xInspect.WordEditor

    Worksheets("SHEET2").Range("C44:AF71").Copy

    pageEditor.Application.Selection.start = 1
    pageEditor.Application.Selection.End = pageEditor.Application.Selection.start
    pageEditor.Application.Selection.PasteAndFormat (wdChartPicture)
    pageEditor.Application.Selection.InsertParagraphAfter

    Worksheets("SHEET2").Range("C26:AF44").Copy

    pageEditor.Application.Selection.PasteAndFormat (wdChartPicture)
    pageEditor.Application.Selection.InsertParagraphAfter           
    .Display
    email.HTMLBody = "SOME TEXT " _
     & email.HTMLBody & " some text"
              
    Set pageEditor = Nothing
    Set xInspect = Nothing
End With

Set email = Nothing
Set outlook = Nothing
Application.CutCopyMode = False
End Sub

【问题讨论】:

标签: excel vba


【解决方案1】:

在 VBA 中创建电子邮件正文。

把它放在一个变量中,包括你所有的段落和换行符。然后将那个变量用于

email.HTMLBody = varEmailBody

编辑:如果您使用.HTMLBody,则无法复制/粘贴任何内容。您需要在某处(在您的代码中)将 HTML 构造为文本。 Ron de Bruin 有各种 Excel 到 Outlook 电子邮件方案的优秀示例。这是一个用于在 HTML 正文中邮寄 Excel 范围的方法:https://www.rondebruin.nl/win/s1/outlook/bmail2.htm -- 您可能需要在工作表中构建一个连续的范围(可能在隐藏的工作表上),您可以在 VBA 中将其作为一个范围进行处理。

【讨论】:

  • 但是如何将复制范围添加到变量并超过它们?
  • 好的,我知道了,但是:1. 它没有复制条件格式,2. 没有添加我的签名。知道怎么改吗?
  • 也许你需要看一下文档,看看有什么可能并调整你的期望。 1. 根据定义,条件格式需要对单元格进行评估(通过 Excel)并且格式是动态应用的。如果单元格从 Excel 应用程序中取出,Excel 应该如何进入 Outlook 并对其进行格式化? - 和 2. 签名不是正文的一部分。有一个使用默认签名甚至特定签名的命令,但我不知道它是否在我的脑海中。我将不得不在文档中查找它。所以,我希望你也能做到。
  • @vipmaciej 如果您使用 HTML 制作单元格范围,您可以为单元格的背景着色以匹配工作表。否则,如果截图没问题,也许你可以抓住 Excel 窗口的 DC 并将其添加为内联图片?
猜你喜欢
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2021-03-28
  • 1970-01-01
  • 2019-08-02
  • 2020-05-14
相关资源
最近更新 更多