将不同工作表中的多个strbodies添加到活动电子邮件中

时间:2018-02-25 16:51:29

标签: vba excel-vba email range excel

当前代码我打开了一封电子邮件,其中包含1个工作簿中所选工作表的特定单元格范围。但是,每次激活代码时,它都会将单元格区域放在一个全新的电子邮件中。

我可以知道如何修改下面的代码以便在不同的工作簿中使用,这样每次我在每个工作簿中单独激活宏时,它会将(工作簿的)特定单元格范围添加到同一个活动的电子邮件中而不是一个新的电子邮件(即根据我的光标在同一个有效电子邮件中的位置添加激活宏的不同书籍的单元格范围)?

希望我足够清楚。谢谢一位工厂!

Sub Trigger_Email()

Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String

StrBody = "Hello John," & "<br>" & "<br>" & _
"The company provides" & "<br>" & "<br>" & _
"For your consideration& views." & "<br>" & "<br>"

Set rng = Nothing

On Error Resume Next

'Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want

Set rng = Sheets("ClaimEx").Range("A5:G22").Rows.SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Comp").Range("A1:F19").Rows.SpecialCells(xlCellTypeVisible)
Set rng3 = Sheets("TPO").Range("A1:F17").Rows.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 OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next

With OutMail
    .To = "xxx@hotmail.com"
    .CC = ""
    .BCC = ""
    .Subject = "Fleet Quotation - " & Cells(5, 1)
    .HTMLBody = StrBody & rangetoHTML(rng) & rangetoHTML(rng2) & rangetoHTML(rng3)
    .Display   '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"

'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

0 个答案:

没有答案