Excel宏来填充电子邮件正文中的表

时间:2018-02-15 09:37:37

标签: excel vba excel-vba outlook outlook-vba

我是编码的初学者,并尝试创建一个宏来向我的团队中的信用分析师发送电子邮件。我正在使用示例Excel工作表来获取数据。

如何在示例数据中列出的每个信用分析师的电子邮件正文中添加4x2表?下面是一个非常基本的代码,我可以在花费数小时后创建。

Sub Credit_Auto()
    Dim objOutlook As Object
    Dim objMail As Object
    Dim Ws As Worksheet
    Dim strbody As String

    Set objOutlook = CreateObject("Outlook.Application")
    Set Ws = ActiveSheet

    Dim rCell As Range
    For Each rCell In Ws.Range("G2", Ws.Range("G1000").End(xlUp))
        Debug.Print rCell.Address
        Set objMail = objOutlook.CreateItem(0)

        strbody = "Dear " & rCell.Offset(0, -1).Value & "," & "<br>" & "<br>" & _
          "Please allocate the below account to it's appropriate parent account." & "<br>" & "<br>" & _
          "Regards" & "<br>" & "<br>" & _
          "Ankit"

        With objMail
            .To = rCell.Value
            .Subject = "Unalloctaed Credit Profiles"
            .HTMLBody = strbody
            .Send
        End With

    Next rCell

    Set objMail = Nothing
    Set Ws = Nothing
    Set objOutlook = Nothing
End Sub

我需要在strbody中的第二行之后的表格(该表格应该包含A到D的数据,包含标题和F列中分析人员的相应行。)

以下是示例数据Click Here

  

enter image description here

对于第一封电子邮件,最终的电子邮件应如下所示..

enter image description here

  

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试下面的脚本。它将允许您选择附加到Outlook电子邮件正文的范围。

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    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("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 OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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

请注意:Function RangetoHTML(rng As Range)