简体   繁体   中英

Generate email in Outlook using VBA from directory

I need to generate a series of emails that attach pdf files from specific folders. I am a novice but have some understanding of the code that I'm using. My problem is that I cannot control the number of emails being generated. I want to be able to generate the exact number of emails that there are entries in my directory (rows).

在此处输入图片说明

This is the code, any help would be greatly appreciated:

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

Is this what you are trying? ( Untested )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM