如何在此现有代码中将嵌入式图片(html)和.cc添加到电子邮件中

时间:2019-07-07 14:37:31

标签: excel vba

为此奋斗了几天:) 我有一张excel表,上面有过期的员工和课程,我需要通知这些员工他们已经过期并抄送他们的经理。我还在电子邮件正文中使用了屏幕截图。我改编了Ron de Bruin- https://www.rondebruin.nl/win/s1/outlook/bmail5.htm中的代码。 我想在电子邮件的正文中或作为附件添加员工应届课程。

我尝试为每封电子邮件组合用于行附件的代码,但是失败了。 罗恩·德·布鲁因(Ron de Bruin)的代码似乎很适合该表(示例2 https://www.rondebruin.nl/win/s1/outlook/bmail8.htm),但我不知道如何添加我今天使用的.cc和内联图片。我尝试更改代码,但我想我没有正确获取语法。 还不确定如何处理2个附件吗?是否创建附件数组并分别定义它们(附件(1)表,附件(2)内嵌图像?

`Sub Send_Row_Or_Rows_Attachment_2()
'Working in 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim rng As Range
    Dim Ash As Worksheet
    Dim Cws As Worksheet
    Dim Rcount As Long
    Dim Rnum As Long
    Dim FilterRange As Range
    Dim FieldNum As Integer
    Dim NewWB As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    'for the CC I added the MngrEmail below
    Dim MngrEmail As String



    On Error GoTo cleanup
    Set OutApp = CreateObject("Outlook.Application")

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    'Set filter sheet, you can also use Sheets("MySheet")
    Set Ash = ActiveSheet

    'Set filter range and filter column (column with e-mail addresses)
    Set FilterRange = Ash.Range("A1:AP" & Ash.Rows.Count)
    FieldNum = 40    '(my email is in column AN) Filter column = B because the filter range start in column A


    'Add a worksheet for the unique list and copy the unique list in A1
    Set Cws = Worksheets.Add
    FilterRange.Columns(FieldNum).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Cws.Range("A1"), _
            CriteriaRange:="", Unique:=True

    'Count of the unique values + the header cell
    Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

    'If there are unique values start the loop
    If Rcount >= 2 Then
        For Rnum = 2 To Rcount

            'If the unique value is a mail addres create a mail
            If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

                'Filter the FilterRange on the FieldNum column
                FilterRange.AutoFilter Field:=FieldNum, _
                                       Criteria1:=Cws.Cells(Rnum, 1).Value



                'Copy the visible data in a new workbook
                With Ash.AutoFilter.Range
                    On Error Resume Next
                    Set rng = .SpecialCells(xlCellTypeVisible)
                    On Error GoTo 0
                End With

                Set NewWB = Workbooks.Add(xlWBATWorksheet)

                rng.Copy
                With NewWB.Sheets(1)
                    .Cells(1).PasteSpecial Paste:=8
                    .Cells(1).PasteSpecial Paste:=xlPasteValues
                    .Cells(1).PasteSpecial Paste:=xlPasteFormats
                    .Cells(1).Select
                    Application.CutCopyMode = False
                End With

                'Create a file name
                TempFilePath = Environ$("temp") & "\"
                TempFileName = "Your data of " & Ash.Parent.Name _
                             & " " & Format(Now, "dd-mmm-yy h-mm-ss")

                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2016
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If

                'Save, Mail, Close and Delete the file
                Set OutMail = OutApp.CreateItem(0)

                With NewWB
                    .SaveAs TempFilePath & TempFileName _
                          & FileExtStr, FileFormat:=FileFormatNum
                    On Error Resume Next
                    With OutMail
                        .to = Cws.Cells(Rnum, 1).Value
'added the .cc here
                        .cc = MngrEmail
                        .Subject = "Test mail"
                        .Attachments.Add NewWB.FullName
                        .Body = "Hi there"
                        .Display  'Or use Send
                    End With
                    On Error GoTo 0
                    .Close savechanges:=False
                End With

                Set OutMail = Nothing
                Kill TempFilePath & TempFileName & FileExtStr
            End If

            'Close AutoFilter
            Ash.AutoFilterMode = False

        Next Rnum
    End If

cleanup:
    Set OutApp = Nothing
    Application.DisplayAlerts = False
    Cws.Delete
    Application.DisplayAlerts = True

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub`

有人可以帮忙吗? 谢谢!

0 个答案:

没有答案