Excel选定范围到Outlook电子邮件正文-格式问题

Excel选定范围到Outlook电子邮件正文-格式问题,excel,vba,email,outlook,Excel,Vba,Email,Outlook,我花了几个小时在论坛上寻求帮助。但是,我的VBA水平还不足以实现和测试代码中的更改 简而言之,我有一个excel文件,我想通过outlook电子邮件发送选定的范围。 这里有很多教程,效果很好 但我的问题是格式。 无论我如何尝试,outlook电子邮件中的行高总是乱七八糟,图形与表格重叠等等。尽管行宽和对象位置都可以 那么,有没有什么诀窍,如何保持格式与excel文件中的格式完全相同 以下是通过电子邮件发送有效范围的代码: Private Sub Workbook_Open() ActiveWo

我花了几个小时在论坛上寻求帮助。但是,我的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

您可以参考以下代码:

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
函数范围到HtmlFlexWidth(rng作为范围)
“由Ron de Bruin于2006年10月28日更改
“2000-2013年在办公室工作
作为对象的Dim fso
将T作为对象
将文件设置为字符串
将TempWB设置为工作簿
TempFile=Environ$(“temp”)和“\”格式(现在是“dd-mm-yy h-mm-ss”)和“.htm”
'复制范围并创建一个新工作簿,以超过中的数据
收到
Set TempWB=工作簿。添加(1)
带临时工作表(1)
.单元格(1).粘贴特殊粘贴:=8
.单元格(1).粘贴特殊值,False,False
.单元格(1).粘贴特殊xlPasteFormats,False,False
.单元格(1)。选择
Application.CutCopyMode=False
出错时继续下一步
.DrawingObjects.Visible=True
.DrawingObjects.Delete
错误转到0
以
'将工作表发布到htm文件
使用TempWB.PublishObjects.Add(_
SourceType:=xlSourceRange_
文件名:=临时文件_
工作表:=临时工作表(1).名称_
来源:=TempWB.Sheets(1).UsedRange.Address_
HtmlType:=xlHtmlStatic)
.发布(真实)
以
'将htm文件中的所有数据读入RangetoHTML
设置fso=CreateObject(“Scripting.FileSystemObject”)
设置ts=fso.GetFile(TempFile).OpenAsTextStream(1,-2)
RangetoHTMLFlexWidth=ts.readall
关闭
RangetoHTMLFlexWidth=Replace(RangetoHTMLFlexWidth,“align=center x:publishsource=”_
“align=left x:publishsource=”)
暗星指数和长星指数一样
同长指数
作为字符串的Dim子字符串
'将表格宽度更改为“100%”

startIndex=InStr(RangetoHTMLFlexWidth),“谢谢,这很好。但是发送图形有问题,它们不包括在选择中。也许这个链接会对您有所帮助:好吧,如果我愿意发送整个内容(范围A1:X600)作为图片。此代码可以工作,但图像会干扰图形中的文本和表格中的粗体文本,因此无法读取。实际上,最好的方法是使用MailEnvelope态度,但有一些条件,它将保持图形的位置和行的高度。