【发布时间】:2018-08-06 19:52:58
【问题描述】:
我希望能够发送一封包含 Excel 电子表格中的单元格的电子邮件。目前我有以下代码将我想要的范围插入到电子邮件中,但我遇到的问题是它删除了大部分格式,例如字体发生变化,一些条件格式被删除。
Sub EmailExtract()
Dim objOutlook As Object
Dim objMail As Object
Dim TempFilePath As String
Dim Location As String
Dim Individual As String
Dim rng As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
Worksheets("Contacts").Activate
Range("A2").Select
While ActiveCell <> ""
ActiveCell.Offset(1, 0).Select
Location = ActiveCell.Address
Individual = ActiveCell.Value
Worksheets("Individual Output 2").Activate
Range("C2").Value = Individual
Set rng = ActiveSheet.Range("A1:M28").Rows.SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With objMail
.To = "joe.bloggs@hotmail.com"
.Subject = ""
Dim Greeting As String
If Time >= #12:00:00 PM# Then
Greeting = "Afternoon ,"
Else
Greeting = "Morning,"
End If
.HTMLBODY = "<font face=Arial><p>" & "Good " + Greeting + "</p>"
.HTMLBODY = .HTMLBODY + "<p>" & "Please find below your " & MonthName((Month(Date)) - 1) & " Information." & "</p>"
.HTMLBODY = .HTMLBODY + RangetoHTML(rng)
.HTMLBODY = .HTMLBODY + "<p>" & "Kind Regards" & "</p>"
.HTMLBODY = .HTMLBODY + "<p>" & "Joe Bloggs" & "</p></font>"
.Display
End With
Worksheets("Contacts").Activate
Wend
Set objOutlook = Nothing
Set objMail = Nothing
Set objOutlook = Nothing
Set objMail = Nothing
End Sub
Function RangetoHTML(rng As Range)
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
我想要的是能够通过电子邮件发送应用格式的摘录,可以这样做吗?也许通过将其作为图片粘贴到电子邮件中?
【问题讨论】:
-
我使用了 Ron De Bruin 的函数并按照 Kemo 的建议进行了交换,除了提供单元格宽度外,它都可以正常工作。添加以下内容后,我完全按照自己的意愿在电子邮件中查找: .Cells(1).PasteSpecial Paste:=xlPasteColumnWidths