【问题标题】:Copy excel table to outlook mail and keep the format将excel表格复制到outlook邮件并保留格式
【发布时间】:2018-02-02 01:04:29
【问题描述】:

我正在使用 VBA 在 excel 中发送一系列单元格的电子邮件。当我复制到 Outlook 时,表格的大小变得混乱,所有文本都被包装起来。

我想保持我的表格的相同格式和大小,我尝试复制为图片,但图片变得非常小。来自A1:AP98

有人可以帮忙吗?我正在使用 Microsoft Office 2010

下面是我的代码

Sub SendEmail()

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.Display

    olMail.To = "xxxx@xxxx.com"
    olMail.Subject = "Subject Line"
    olMail.HTMLBody = "Hello," & vbNewLine & vbNewLine & _
            "Welcome to My World" & vbNewLine & vbNewLine & _
            RangetoHTML(ActiveSheet.Range("A1:Ap90")) & _
           "Thank you for your cooperation." & "<br>" & olMail.HTMLBody
'    olMail.Send

End Sub

【问题讨论】:

  • 我几乎可以肯定你(不)在.htmlBody 中使用 html 编码的方式。用&lt;p&gt; 和/或&lt;br&gt; 语句替换vbNewLine。这也是一个很大的范围(假设列大小不是超级小)。

标签: vba excel email outlook


【解决方案1】:

将其粘贴为 HTML 对象。

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    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 Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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】:

    你可以通过分解来做到这一点。

    首先,您需要将所需的数据提取到新工作表中。您可以稍后删除工作表。

    如果你也想复制格式,你可以使用 类似ThisWorkbook.Sheets("Copy").Range("A1").PasteSpecial Paste:=xlPasteFormats。您可能需要xlPasteFormatsxlPasteColumnWidthsxlPasteValues

    创建新工作表:Sheets.Add(, Sheets(Sheets.Count)).name = "worksheetName"

    邮件信封示例

    Sub sendEmail()
    
    ThisWorkbook.EnvelopeVisible = True
    
    With ThisWorkbook.Sheets("Copy").MailEnvelope
      .Introduction = "This is the email message"
      .Item.To = "abc@domain.com"
      .Item.Subject = "Subject"
      .Item.Send
    
    End With
    
    ThisWorkbook.EnvelopeVisible = False
    
    'Delete the the worksheet
    ThisWorkbook.Sheets("Copy").Delete
    
    End Sub
    

    在这里阅读一些好的指南:https://www.rondebruin.nl/win/s1/outlook/mail.htm

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多