且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在邮件正文中复制粘贴excel表并使用vbscript发送邮件

更新时间:2022-10-16 22:07:50

(temp)&a熔点; \&格式(现在,dd-mm-yy h-mm-ss)& .htm





'复制范围并创建一个新工作簿来过去数据

rng.Copy

设置TempWB = Workbooks.Add(1)

使用TempWB.Sheets(1)

。细胞(1).PasteSpecial膏:= 8

.Cells(1).PasteSpecial xlPasteValues,FALSE,FALSE

.Cells(1).PasteSpecial xlPasteFormats,FALSE,FALSE

。细胞(1)。选择

Application.CutCopyMode = False

On Error Resume Next

.DrawingObjects.Visible = True

.DrawingObjects.Delete

错误GoTo 0

结束





'将工作表发布到htm文件

使用TempWB.PublishObjects.Add(_

SourceType:= xlSourceRange,_

文件名:= TempFile,_

工作表:= TempWB.Sheets(1).Name,_

来源:= TempWB.Sheets(1) .UsedRange.Address,_

HtmlType:= xlHtmlStatic)

。发布(True)

结束





'将htm文件中的所有数据读入RangetoHTML

设置fso = CreateObject(Scripting.FileSystemObject)

将TS = fso.GetFile(临​​时文件).OpenAsTextStream(1,-2)

RangetoHTML = ts.readall

ts.Close

RangetoHTML =替换(RangetoHTML,align = center x:publishsource =,_

align = left x:publishsource =)





'关闭TempWB

TempWB.Close savechanges:= False





'删除我们在此函数中使用的htm文件

杀死TempFile





设置ts =没有什么

设置fso =没什么

设置TempWB = Nothing

结束功能


How to copy paste excel table in mail body and send mail using vbscript

I have excel sheet table and i need to send this table in email.

i tried in vba but in vbscript i unable to do

Need help..!!

What I have tried:

On Error Resume Next


Set rng = Sheets("sheet2").Range("D2:L9").SpecialCells(xlCellTypeVisible)


On Error GoTo cleanup



Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "vijaysinh.d@tcs.com"
.Subject = "Remedy ticket status"
.HTMLBody = StrBody & RangetoHTML(rng)
.Attachments.Add ("Z:\21 Warroom 1\Open Ticket Dump\2019\May\24 May\data\Impact_Sheet_CRQ000000095836.xls")
.Send
End With
On Error GoTo 0
Set OutMail = Nothing


cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True


End Sub
Function RangetoHTML(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)
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

("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