Excel 将两个不同工作簿中的两个区域粘贴到Outlook邮件正文中
我必须将两个选定范围从两个不同的工作簿粘贴到邮件正文中 第一个工作簿是宏所在的父工作簿。我们从中选择一个范围,通过宏打开第二个工作簿并选择一个范围 这两个范围都将粘贴到Outlook邮件正文中 我尝试了下面的宏和rangetohtml函数Excel 将两个不同工作簿中的两个区域粘贴到Outlook邮件正文中,excel,vba,outlook,range,Excel,Vba,Outlook,Range,我必须将两个选定范围从两个不同的工作簿粘贴到邮件正文中 第一个工作簿是宏所在的父工作簿。我们从中选择一个范围,通过宏打开第二个工作簿并选择一个范围 这两个范围都将粘贴到Outlook邮件正文中 我尝试了下面的宏和rangetohtml函数 Sub Mail_Selection_Range_Outlook_Body() ' You need to use this module with the RangetoHTML subroutine. ' Works in Excel 2000, Exce
Sub Mail_Selection_Range_Outlook_Body()
' 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 rng2 As Range
Dim OutApp As Object
Dim OutMail As Object
Set Parent_wkb = ThisWorkbook
cnt_row = Parent_wkb.Worksheets("1Summary - All").Cells(Rows.Count, 1).End(xlUp).Row
cnt_col = Parent_wkb.Worksheets("1Summary - All").Cells(3, Columns.Count).End(xlToLeft).Column
l1 = cnt_row + 7
last_box = cnt_col - 2
last_box_Ltr = Evaluate("substitute(address(1, " & last_box & ", 4), ""1"", """")")
r1 = "B2:" & last_box_Ltr & l1
hide1 = cnt_col - 10
hide1_cell = Evaluate("substitute(address(1, " & hide1 & ", 4), ""1"", """")")
hide2 = cnt_col - 11
hide2_cell = Evaluate("substitute(address(1, " & hide2 & ", 4), ""1"", """")")
Worksheets("1Summary - All").Columns(hide2_cell & ":" & hide1_cell).Select
Selection.EntireColumn.Hidden = True
Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Worksheets("1Summary - All").Range(r1).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 rng_dt = Worksheets("1Summary - All").Range("AGQ2")
dt = rng_dt.Cells(1, 1).Value
dt_formatted1 = Format(dt, "dd-MMMM-yyyy")
dt_formatted2 = Format(dt, "dd MMMM yyyy")
' folder = Mid(dt, 2, 4)
'open the global variable file to get the acu file name along with the file name to be attached
'Attach_xcl - an excel workbook to be attached (working fine)
'body_xcl - another excel workbook, from where I have to select the range
'both the workbook names along with their path are stored in this global variable file(working fine)
Set wkb1 = Workbooks.Open(Filename:="\\vfpnbrgspr0\LESR_Phase2_R_RPA\Sayan\Global_Variable_Singapore.xlsx")
Set rng_xcl1 = wkb1.Worksheets("Global_Variable").Range("B17")
Attach_xcl = rng_xcl1.Cells(1, 1).Value
Set rng_xcl2 = wkb1.Worksheets("Global_Variable").Range("B18")
Body_xcl = rng_xcl2.Cells(1, 1).Value
ActiveWorkbook.Close
'open the acu limit(Body_xcl) file for second table for mail body
Set wkb2 = Workbooks.Open(Filename:=Body_xcl)
Set rng2 = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng2 = wkb2.Worksheets("Biz Breakdown").Range("body").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 rng2 Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
wkb2.Close
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
wkb2.Close
'------------------------------PREPARE THE MAIL TO BE SENT---------------------------------------------'
Dim StrBody1 As String
' Build the string that you want to add.
StrBody1 = "Dear All," & "<br><br>" & _
"This is confidential." & "<br><br><br>"
Dim StrBody2 As String
' Build the string that you want to add.
StrBody2 = " " & "<br><br><br><br>" & _
"Thanks," & "<br>" & _
"user" & "<br>"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
' .To = "xxxx"
.To = "xxxx"
.CC = "xxxx"
.BCC = "xxxx"
.Subject = "This is confidential" & dt_formatted2
'THE LINE BELOW IS NOT WORKING, IF I WORK WITH 'rng' ITS WORKING FINE, WHEN I ADD THE SECOND 'rng2' THE BODY OF THE MAIL IS BLANK
.HTMLBody = StrBody1 & RangetoHTML(rng) & " " & RangetoHTML(rng2) & StrBody2
'.HTMLBody = RangetoHTML(rng2) & StrBody2
.Attachments.Add (Attach_xcl)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Worksheets("1Summary - All").Columns(hide2_cell & ":" & hide2_cell).Select
Selection.EntireColumn.Hidden = False
Worksheets("1Summary - All").Columns(hide1_cell & ":" & hide1_cell).Select
Selection.EntireColumn.Hidden = False
End Sub
Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
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 workbook to receive the data.
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 an .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 the RangetoHTML subroutine.
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.
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Sub-Mail\u Selection\u Range\u Outlook\u Body()
'您需要将此模块与RangetoHTML子例程一起使用。
'在Excel 2000、Excel 2002、Excel 2003、Excel 2007、Excel 2010、Outlook 2000、Outlook 2002、Outlook 2003、Outlook 2007和Outlook 2010中工作。
变暗rng As范围
变暗rng2 As范围
Dim OutApp作为对象
将邮件变暗为对象
设置父项_wkb=此工作簿
cnt_row=父工作表(“1汇总-全部”)。单元格(Rows.Count,1)。结束(xlUp)。行
cnt_col=Parent_wkb.Worksheets(“1汇总-全部”)。单元格(3,Columns.Count)。结束(xlToLeft)。列
l1=cnt_行+7
最后一个字符框=cnt字符列-2
last_box_Ltr=评估(“替换(地址(1),&last_box&”,4),“1”))
r1=“B2:”&最后一个\u框\u Ltr&l1
hide1=cnt_列-10
hide1_cell=Evaluate(“替换(地址(1,&hide1&)”,4),“1”)
hide2=cnt_列-11
hide2_cell=Evaluate(“替换(地址(1,&hide2&)”,4),“1”)
工作表(“1摘要-全部”)。列(隐藏单元格和“:”&隐藏单元格)。选择
Selection.EntireColumn.Hidden=True
设置rng=无
出错时继续下一步
'仅发送所选内容中的可见单元格。
设置rng=工作表(“1汇总-全部”)。范围(r1)。特殊单元格(xlCellTypeVisible)
'您还可以将范围与以下语句一起使用。
'设置rng=工作表(“您的工作表”).范围(“D4:D12”).特殊单元格(xlCellTypeVisible)
错误转到0
如果rng不算什么,那么
MsgBox“所选内容不是范围或工作表受保护。”&_
vbNewLine&“请更正并重试。”,vbOKOnly
出口接头
如果结束
应用
.EnableEvents=False
.ScreenUpdate=False
以
设置rng_dt=工作表(“1汇总-全部”)。范围(“AGQ2”)
dt=rng_dt.单元格(1,1).值
dt_formatted1=格式(dt,“dd-MMMM-yyyy”)
dt_formatted2=格式(dt,“dd-MMMM-yyyy”)
'文件夹=Mid(dt,2,4)
'打开全局变量文件以获取acu文件名以及要附加的文件名
'附加\u xcl-要附加的excel工作簿(工作正常)
'body_xcl-另一个excel工作簿,我必须从中选择范围
'工作簿名称及其路径都存储在此全局变量文件中(正常工作)
设置wkb1=工作簿。打开(文件名:“\\vfpnbrgspr0\LESR\u Phase2\u R\u RPA\Sayan\Global\u Variable\u Singapore.xlsx”)
设置rng_xcl1=wkb1.工作表(“全局变量”).范围(“B17”)
附加\u xcl=rng\u xcl1.Cells(1,1).Value
设置rng\U xcl2=wkb1.工作表(“全局变量”).范围(“B18”)
Body_xcl=rng_xcl2.Cells(1,1).Value
活动工作簿。关闭
'打开邮件正文第二个表的acu限制(Body_xcl)文件
设置wkb2=Workbooks.Open(文件名:=Body\u xcl)
设置rng2=无
出错时继续下一步
'仅发送所选内容中的可见单元格。
设置rng2=wkb2.工作表(“业务细分”).范围(“正文”).特殊单元格(xlCellTypeVisible)
'您还可以将范围与以下语句一起使用。
'设置rng=工作表(“您的工作表”).范围(“D4:D12”).特殊单元格(xlCellTypeVisible)
错误转到0
如果rng2什么都不是,那么
MsgBox“所选内容不是范围或工作表受保护。”&_
vbNewLine&“请更正并重试。”,vbOKOnly
wkb2.关闭
出口接头
如果结束
应用
.EnableEvents=False
.ScreenUpdate=False
以
wkb2.关闭
“------------------------------------准备要发送的邮件-----------------------------------------”
作为字符串的Dim StrBody1
'生成要添加的字符串。
StrBody1=“亲爱的大家,&”
”和_
“这是保密的。”&“
”
Dim StrBody2作为字符串
'生成要添加的字符串。
StrBody2=“”&“
”和_
“谢谢,&”
“&”_
“用户”&“
”
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
发邮件
'.To=“xxxx”
.To=“xxxx”
.CC=“xxxx”
.BCC=“xxxx”
.Subject=“这是机密的”&dt\u格式2
'下面的行不起作用,如果我使用'rng'它工作正常,当我添加第二个'rng2'时,邮件正文是空白的
.HTMLBody=strobody1&RangetoHTML(rng)&“&RangetoHTML(rng2)&strobody2
'.HTMLBody=RangetoHTML(rng2)和StrBody2
.Attachments.Add(附加)
'代替下面的语句,您可以使用“.Display”来
'显示电子邮件消息。
.发送
以
错误转到0
应用
.EnableEvents=True
.ScreenUpdate=True
以
发送邮件=无
设置应用程序=无
工作表(“1摘要-全部”)。列(隐藏单元格和“:”&隐藏单元格)。选择
Selection.EntireColumn.Hidden=False
工作表(“1摘要-全部”)。列(隐藏单元格和“:”&隐藏单元格)。选择
Selection.EntireColumn.Hidden=False
端接头
函数RangetoHTML(rng作为范围)
'在Excel 2000、Excel 2002、Excel 2003、Excel 2007、Excel 2010、Outlook 2000、Outlook 2002、Outlook 2003、Outlook 2007和Outlook 2010中工作。
作为对象的Dim fso
将T作为对象
将文件设置为字符串
将TempWB设置为工作簿
TempFile=Environ$(“temp”)&“/”和格式(现在是“dd-mm-yy h-mm-ss”)&.htm”
'复制范围并创建工作簿以接收数据。
收到
设置TempWB=工作
If rng2 Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
wkb2.Close
Exit Sub
Else
Dim HtmlRng2 as string
HtmlRng2 = RangeToHtml(rng2)
End If
.HTMLBody = StrBody1 & RangetoHTML(rng) & " " & RangetoHTML(rng2) & StrBody2
.HTMLBody = StrBody1 & RangetoHTML(rng) & " " & HtmlRng2 & StrBody2