首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将excel表格复制到outlook邮件中并保留格式

将excel表格复制到outlook邮件中并保留格式
EN

Stack Overflow用户
提问于 2018-02-02 09:04:29
回答 2查看 22.3K关注 0票数 0

我正在使用VBA在excel中发送一系列单元格的电子邮件。当我复制到outlook时,表格的大小变得乱七八糟,所有的文本都被换行。

我想保持我的表格的格式和大小不变,并且我试图复制为图片,但图片变得非常小。是A1:AP98寄来的。

有人能帮上忙吗?我正在使用Microsoft office 2010

下面是我的代码

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-03 04:01:09

将其粘贴为HTML对象。

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2018-02-02 10:10:32

你可以通过分解成几个部分来实现。

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

如果您还希望复制该格式,则可以使用类似于ThisWorkbook.Sheets("Copy").Range("A1").PasteSpecial Paste:=xlPasteFormats的内容。你可能需要xlPasteFormatsxlPasteColumnWidths & xlPasteValues

要创建新工作表,请执行以下操作:Sheets.Add(, Sheets(Sheets.Count)).name = "worksheetName"

MailEnvelop示例

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48574223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档