使用目录

时间:2017-06-02 12:34:35

标签: excel vba excel-vba outlook

我需要生成一系列附加特定文件夹的pdf文件的电子邮件。我是新手但对我使用的代码有一定的了解。我的问题是我无法控制正在生成的电子邮件数量。我希望能够生成我的目录(行)中有条目的确切电子邮件数。

enter image description here

这是代码,非常感谢任何帮助:

Sub create_email()
    'On Error Resume Next
    'Dim oMail As Outlook.MailItem`
    'Dim num_clients, start_row As Integer`

    Sheets("Control").Activate
    start_row = Range("start_row").row
    num_clients = Range("B100").End(xlUp).row - start_row

    For i = 1 To num_clients
        Set oMail = Outlook.Application.CreateItem(olMailItem)

        'Subject line
        oMail.Subject = Range("J9").Offset(i - 1, 0)

        'Distribution list
        Set RecipTo = oMail.Recipients.Add(Range("K9").Offset(i - 1, 0))
        RecipTo.Type = olTo
        Set RecipCC = oMail.Recipients.Add(Range("L9").Offset(i - 1, 0))
        RecipCC.Type = olCC
        oMail.SentOnBehalfOfName = "email@email.com.au"
        oMail.Recipients.ResolveAll

        'Attachments + message
        oMail.Attachments.Add Range("E9").Offset(i - 1, 0) & "\" & Range("F9").Offset(i - 1, 0)
        oMail.HTMLBody = "<html><p><font face=""Calibri""><font size=3>Dear Sir/ Madam,</p>" & _
                   "<html><p><font face=""Calibri"">Kind regards,</p>"

        'Displays email pre-send
        oMail.Display
        Sheets("Control").Activate

        Set oMail = Nothing
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

这是你在尝试什么? (的未测试

Sub create_email()
    Dim OutApp As Object, oMail As Object
    Dim wb As Workbook, ws As Worksheet
    Dim i As Long, start_Rows As Long, Last_Row As Long

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Control")

    With ws
        start_Row = .Range("start_row").Row '<~~ Start Row
        Last_Row = .Range("B" & .Rows.Count).End(xlUp).Row '<~~ End Row

        Set OutApp = CreateObject("Outlook.Application")

        For i = start_Row To Last_Row '<~~ Loop from start row to end row
            Set oMail = OutApp.CreateItem(0)

            With oMail
                .Subject = ws.Range("I" & i).Value
                .To = ws.Range("J" & i).Value
                .Cc = ws.Range("K" & i).Value
                .SentOnBehalfOfName = "email@email.com.au"
                .Attachments.Add ws.Range("D" & i).Value & "\" & ws.Range("E" & i).Value
                .HTMLBody = "<html><p><font face=""Calibri""><font size=3>Dear Sir/ Madam,</p>" & _
                            "<html><p><font face=""Calibri"">Kind regards,</p>"

                .Display
            End With
        Next i
    End With
End Sub