vba 在 Outlook 电子邮件中粘贴 Excel 表格:回顾过去
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25023530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pasting Excel Table in an Outlook email: it looks back
提问by user3889057
I have been able to run the following code for copying an Excel range into an Outlook email (using the code provided by Ron de Bruin:
我已经能够运行以下代码将 Excel 范围复制到 Outlook 电子邮件中(使用 Ron de Bruin 提供的代码:
Sub SendEMail(SheetName As String, EmailBody As String, EmailSubject As String, MyAttachment As String)
' You need to use this module with the RangetoHTML subroutine.
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
ActiveSheet.Unprotect
Set rng = ActiveSheet.Range(EmailBody).SpecialCells(xlCellTypeVisible)
' You can also use a range with the following statement.
' 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 = "[email protected]"
'.CC = ""
'.BCC = ""
.subject = "Resumen de " & EmailSubject
.htmlbody = RangetoHTML(rng)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
ActiveSheet.Protect
End Sub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
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).PasteSpecial xlPasteAll
.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
It works fine for sending, but problem is when opening. The table appears too wide for the reader
它可以正常发送,但问题是打开时。表格对读者来说太宽了
Is there anything that can be done to correct this and have the columns with the same width than in Excel?
有什么办法可以纠正这个问题并使列的宽度与 Excel 中的相同吗?
Thanks
谢谢
回答by user3514930
I think the code are a little complicated, and with this code you can only insert in the mail the values of the range selected...
If you want add the width of the columns, you can add the code:
我认为代码有点复杂,使用此代码您只能在邮件中插入所选范围的值...
如果您想添加列的宽度,您可以添加代码:
With TempWB.Sheets(1)
.Cells(1).PasteSpecial xlPasteAll
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
' Code ADDED
i = 1
For Each xx In rng.Columns
TempWB.Sheets(1).Columns(i).ColumnWidth = xx.ColumnWidth
i = i + 1
Next
' Code ADDED
'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
the only 5 lines between the two Withblock. The code set the original width in the new sheet (temp sheet).
For me it's most simple to copy and paste the Range directly in the HTMLBody of the mail. In that case you have ALL the format of the table (example: color, height, font ...). To do that a portion of code can be:
两个With块之间仅有的 5 行。该代码在新工作表(临时工作表)中设置了原始宽度。
对我来说,将 Range 直接复制并粘贴到邮件的 HTMLBody 中是最简单的。在这种情况下,您拥有表格的所有格式(例如:颜色、高度、字体...)。为此,一部分代码可以是:
Set mailApp = CreateObject("Outlook.Application")
Set mail = mailApp.CreateItem(olMailItem)
mail.Display
mail.To = "[email protected]"
mail.Subject = "subject" & Now
Dim Clip As MSForms.DataObject
Set Clip = New MSForms.DataObject
Clip.SetText ("test ... body" & vbNewLine & vbNewLine _
& "this is another line " & vbCrLf _
& "this is another line again " & vbNewLine & " ")
Clip.PutInClipboard
Set wEditor = mailApp.ActiveInspector.wordEditor
wEditor.Application.Selection.Paste
Selection.Copy
wEditor.Application.Selection.Paste
' mail.send