Excel中的VBA创建带有表格和签名的电子邮件

Posted

技术标签:

【中文标题】Excel中的VBA创建带有表格和签名的电子邮件【英文标题】:VBA In Excel to Create Email with Table and Signature 【发布时间】:2020-06-29 02:26:48 【问题描述】:

我正在尝试在 Excel 中单击按钮时创建自定义电子邮件。所有用户都有 Outlook。在电子邮件的正文中,我想包含已经格式化的电子表格的一部分。

我可以在其中获取信息,但无法在正文中正确获取订单。那是文本,然后是格式化的表格 THEN 签名。

下面的示例将格式化表放在下面,但我希望签名是最后一件事。

任何帮助将不胜感激。

Sub SendUpdateEmail()

Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim EmailTo As String
Dim EmailCC As String
Dim UpdateDate As String
Dim Location As String
Dim strSig As String


' Set Outlook object
Set outlook = CreateObject("Outlook.Application")

' Set Email Mail Object
Set newEmail = outlook.CreateItem(0)

' Set Inspect Object
Set xInspect = newEmail.GetInspector

' Set Page Editor Object
Set pageEditor = xInspect.WordEditor

' Set Email To
EmailTo = Worksheets("Project Summary").Cells(15, "F").Value

' Set Email CC
EmailCC = Worksheets("Project Summary").Cells(16, "F").Value

' Set Update date
UpdateDate = Worksheets("OUTPUT - Daily Field Ticket").Cells(7, "B").Value

' Set Location
Location = Worksheets("OUTPUT - Daily Field Ticket").Cells(5, "B").Value



With newEmail
.To = EmailTo
.CC = EmailCC
.BCC = ""
.Subject = "UPDATE | " + Location + " | " + UpdateDate

'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
.Display

'GET THE html CODE FROM THE SIGNATURE
strSig = .HTMLBody

.HTMLBody = "Hello," & "<br>" & "<br>" & "Please see attached the Daily Field Ticket for " + Location 
+ " for " + UpdateDate + "." + strSig


Sheet1.Range("A28:F35").Copy

pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)

Set pageEditor = Nothing
Set xInspect = Nothing

End With

Set newEmail = Nothing
Set outlook = Nothing

End Sub

【问题讨论】:

【参考方案1】:

当我必须在 Outlook 邮件中复制粘贴范围时,我通常使用 Ron De Bruin 著名的“范围到 HTML”功能。我已将其插入您的代码并进行了一些编辑。它应该会给你预期的结果:

Sub SendUpdateEmail()

Dim outlook As Object
Dim newEmail As Object
Dim EmailTo As String
Dim EmailCC As String
Dim UpdateDate As String
Dim Location As String
Dim strSig As String


' Set Outlook object
Set outlook = CreateObject("Outlook.Application")
' Set Email Mail Object
Set newEmail = outlook.CreateItem(0)
' Set Email To
EmailTo = "test@gmail.com"
' Set Email CC
EmailCC = "test@gmail.com"
' Set Update date
UpdateDate = "18/03/2020"
' Set Location
Location = "Here"

With newEmail
.To = EmailTo
.CC = EmailCC
.BCC = ""
.Subject = "UPDATE | " + Location + " | " + UpdateDate
'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
.Display
'GET THE HTML CODE FROM THE SIGNATURE
strSig = .HTMLBody

.HTMLBody = "Hello," & "<br>" & "<br>" & "Please see attached the Daily Field Ticket for " + Location + _
" for " + UpdateDate + "." + RangetoHTML(Sheet1.Range("A28:F35")) & "<br>" & strSig

.Display

End With

Set newEmail = Nothing
Set outlook = Nothing

End Sub


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

【讨论】:

【参考方案2】:

删除 .HTMLBody 并使用 Page Editor Set pageEditor = xInspect.WordEditor


例子

With newEmail
   .To = EmailTo
   .CC = EmailCC
   .BCC = ""
   .Subject = "UPDATE | " + Location + " | " + UpdateDate

   'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
   .Display

    Worksheets("Sheet1").Range("A28:F35").Copy

    pageEditor.Paragraphs(1).Range.PasteAndFormat (wdFormatPlainText) & vbCr & vbLf

    pageEditor.Range.InsertBefore "Hello," & vbCr & _
                            "Please see attached the Daily Field Ticket for " _
                            + Location + " for " + UpdateDate + "." & vbCr & vbCr



End With

【讨论】:

以上是关于Excel中的VBA创建带有表格和签名的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

VBA Excel - 从 MS Access 将列名保存到电子表格

Excel VBA中的组合算法

在 VBA 中的电子表格对内循环

VBA从Excel中的电子表格中的列表框中获取值

在 Excel 中使用 VBA,如何在 Outlook 中粘贴表格然后将表格转换为文本?

如何在VBA中的用户窗体上使用组合框和文本框来搜索和查找活动Excel电子表格中的数据?