gpt4 book ai didi

excel - 将两个不同工作簿中的两个范围粘贴到 Outlook 邮件正文中

转载 作者:行者123 更新时间:2023-12-04 20:51:43 25 4
gpt4 key购买 nike

我必须将两个选定的范围从两个不同的工作簿粘贴到邮件正文中。

第一个工作簿是宏所在的父工作簿。我们从那里选择一个范围,通过宏打开第二个工作簿并选择一个范围。

这两个范围都将粘贴到 Outlook 邮件正文中。

我尝试了以下宏以及 rangetohtml 函数。

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

最佳答案

您遇到的问题来自您关闭 wkb2 的事实。转换前rng2到 html。当您到达 RangetoHTML(rng2)线,rng2不再可用,如果 wkb2已经关了。

所以你应该转换 rng2在您关闭 wkb2 之前,将其转换为 html 并将其存储在变量中.
你可以这样做:

    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

否则,您将不得不保留 wkb2直到您填写了您的电子邮件 htmlbody与范围。

关于excel - 将两个不同工作簿中的两个范围粘贴到 Outlook 邮件正文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com