【问题标题】:VBA: Copy and paste cells in a email without losing formattingVBA:在电子邮件中复制和粘贴单元格而不会丢失格式
【发布时间】: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

标签: vba excel email outlook


【解决方案1】:

Ron de Bruin's site 上的 RangetoHTML 函数对我来说一直很好用。

您检查过电子邮件的BodyFormat 属性吗?它可能默认为富文本。

【讨论】:

  • 这是我用于代码的,它不认为它显示了如何保持格式
【解决方案2】:

替换这一行:

.Cells(1).PasteSpecial Paste:=8 

与:

.Cells(1).PasteSpecial Paste:=1

& 删除以下两行:

.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2013-03-23
    • 2018-06-25
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多