【问题标题】:Excel selected range to Outlook email body - Formatting problemExcel 选择范围到 Outlook 电子邮件正文 - 格式问题
【发布时间】:2018-11-20 13:14:06
【问题描述】:

我花了几个小时在论坛上寻求帮助。但是我的 VBA 水平还没有达到我能够实现和测试代码更改的水平。

简而言之,我有一个 excel 文件,我想通过 Outlook 电子邮件发送选择的范围。 这里有很多教程,效果很好。

但我的麻烦在于格式。 无论我如何尝试 Outlook 电子邮件中的行高,都会出现混乱,图表会与表格重叠等。不过,行宽和对象位置都可以。

那么有什么诀窍吗,如何保持格式和excel文件中的一模一样呢?

这是通过电子邮件发送范围的代码,它正在工作:

Private Sub Workbook_Open()

ActiveWorkbook.RefreshAll

'Working in Excel 2002-2016
Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range

Sheets("Data").Select

On Error GoTo StopMacro

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

'Fill in the Worksheet/range you want to mail
'Note: if you use one cell it will send the whole worksheet
Set Sendrng = Worksheets("Data").Range("A1:S600")

'Remember the activesheet
Set AWorksheet = ActiveSheet

With Sendrng

    'Select the worksheet with the range you want to send
    .Parent.Select

    'Remember the ActiveCell on that worksheet
     Set rng = ActiveCell

    'Select the range you want to mail
    .Select

    ' Create the mail and send it
      ActiveWorkbook.EnvelopeVisible = True
      With .Parent.MailEnvelope

        ' Set the optional introduction field thats adds
        ' some header text to the email body.
        '.Introduction = "Hello all."

        With .Item
             .To = "xxx@zzz.eu"
             .CC = "xxx@zzz.eu"
             .BCC = ""
             .Subject = "xxx" & Format(Worksheets("Support").Range("A1").Value, "dd.mm.yyyy")
             .Send
        End With

    End With

    'select the original ActiveCell
    rng.Select
End With

'Activate the sheet that was active before you run the macro
AWorksheet.Select

StopMacro:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False

ActiveWorkbook.Save
Application.Quit

End Sub

【问题讨论】:

    标签: excel vba email outlook


    【解决方案1】:

    您可以参考以下代码:

    Function RangetoHTMLFlexWidth(rng As Range)
    ' Changed by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2013
        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)
        RangetoHTMLFlexWidth = ts.readall
        ts.Close
        RangetoHTMLFlexWidth = Replace(RangetoHTMLFlexWidth, "align=center x:publishsource=", _
            "align=left x:publishsource=")
    
        Dim startIndex As Long
        Dim stopIndex As Long
        Dim subString As String
    
        'Change table width to "100%"
        startIndex = InStr(RangetoHTMLFlexWidth, "<table")
        startIndex = InStr(startIndex, RangetoHTMLFlexWidth, "width:") + 5
        stopIndex = InStr(startIndex, RangetoHTMLFlexWidth, "'>")
        subString = Left(RangetoHTMLFlexWidth, startIndex)
        subString = subString & "100%"
        RangetoHTMLFlexWidth = subString & Mid(RangetoHTMLFlexWidth, stopIndex)
    
        '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
    

    更多信息,请参考以下链接:

    Send Excel range into Email body with autofit

    【讨论】:

    • 谢谢,这很好用。但是发送图表有问题,它们不包含在选择中。
    • 也许这个链接对你有帮助:stackoverflow.com/questions/27042842/…
    • 好吧,好吧,以防我放弃将整个东西(范围 A1:X600)作为图片发送。此代码可以工作,但图像会干扰图表中的文本和表格中的粗体文本,因此无法阅读。最好的方法是只使用 MailEnvelope 态度,但在某些条件下,它将保持图形的位置和行的高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多