|
你应该是用的Excel来做表的把,用下面的自定义函数RangetoHTML加入模块中,完了使用发送邮件调用
自定义函数就可以了。勾选VBA的工具—>引用—>microsoft outlook 16.0 object library
Public Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' offic 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" '设置临时网页文件名称
'复制单元格中内容,增加一个临时工作薄保存
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '调整单元格大小,且粘贴格式。
.UsedRange.Value = TempWB.Sheets(1).UsedRange.Value '粘贴为值
.Columns("A:B").Delete Shift:=xlToLeft
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'把选中临时excel中内容发布到HMTL文件上
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
'把所有内容读取到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=") '把中心对齐变为左端对齐
'关闭TempWB且不保存
TempWB.Close SaveChanges:=False
'删除HTM文件
Kill TempFile
'释放内存中临时文件
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
sub 发送邮件()
Dim temp As Object, newmail As Object, strg As String
Set temp = CreateObject("outlook.application")
Set newmail = temp.CreateItem(0) '使用outlook创建新邮件
With newmail
.To = "...@....com" '收件人
.CC = "....@....com" '抄送人
.Subject = '邮件标题
.Body = RangetoHTML() '邮件正文,括号中放入想发邮件的单元格范围
.Attachments.Add “D:/......./123.pdf” '添加附件
.Send
End With |
|