VBA - 在工作簿中调整单元格范围以发送电子邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA - 在工作簿中调整单元格范围以发送电子邮件相关的知识,希望对你有一定的参考价值。
我正在编写一段代码,我发送的每日电子邮件中包含excel文件作为附件,并将活动工作表嵌入到电子邮件正文中。我被要求了解是否有可能让VBA的嵌入式组件适应新的单元格范围。
所以,例如。在一个工作表上,它将包含当天交易活动的数据。如果我想将第2天的交易数据添加到同一工作表中并且只将该部分嵌入到电子邮件中,那么每次在VBA中是否可以不编辑单元格范围?
我在下面附上了我的代码:
Sub Mail_Sheet_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = ActiveSheet.UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "recipient@test.com"
.CC = ""
.BCC = ""
.Subject = "Trades Today" & Date
.htmlBody = RangetoHTML(rng)
.Attachments.Add ActiveWorkbook.FullName
.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)
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:=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
对此有任何建议将不胜感激。
答案
我的回答可能看起来有点具体但我认为从中你至少可以得到我想要的想法。
这就是我正在考虑的'主'表。在这个例子中,它从昨天起没有更新(因此日期是9/12/2018
)。
这是包含今天数据的工作表:
以下脚本将采用今天的数据,向其添加"Date"
列,并填写所有这些行的今天日期。然后它会将“今天”表格中的数据复制到列表顶部的“主”表格中。
Sub Data_Copy()
Dim ws1 As Worksheet
Set ws1 = ActiveSheet 'adjust as necessary
Dim ws2 As Worksheet
Set ws2 = sheets(ws1.index + 1) 'adjust as necessary
Dim lastRow As Long
lastRow = ws2.Range("A" & rows.count).End(xlUp).row
If ws2.Range("A1").Value2 <> "Date" Then
ws2.Columns(1).Insert Shift:=xlRight
ws2.Range("A1").Value2 = "Date"
ws2.Range("A2:A" & lastRow).value = Date
End If
ws2.rows("2:" & lastRow).copy
ws1.rows(2).Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
上一个脚本之后的'今天'表格中的数据添加了"Date"
列:
在今天的数据插入其中后,这是'主'表:
最后,这里是更新的Mail_Sheet_Outlook_Body
子程序,因此只从ActiveSheet
(我之前称之为“主人”)获取今天日期的数据。
现在它在A
列下工作,并且当它找不到该单元格中的今天的日期时获取行号。然后它设置rng
从"A1"
到UsedRange
的最后一列,直到今天的日期的最后一行。
Sub Mail_Sheet_Outlook_Body()
...
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'gets last row with today's date
Dim i As Long
For i = 2 To Range("A" & rows.count).End(xlUp).row
If CDate(Range("A" & i).value) <> Date Then
Exit For
End If
Next i
'makes range out of today's cells
Set rng = Range(Cells(1, "A"), Cells(i - 1, ActiveSheet.UsedRange.Columns.count))
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
...
End Sub
Function RangetoHTML(rng As Range)
...
End Function
以上是关于VBA - 在工作簿中调整单元格范围以发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章