简体   繁体   中英

If statement/Outlook/VBA : Drafting Emails

The purpose of this code is to make an email draft to users who submitted a reclass. The MailTo and Subject are pulled from an excel data table: ecEmailAdresses = 17 and ecSubject = 43 . The line it which I need help on is the ** If Statement ** . I want the macro to only draft up an email if the person submitted a reclass (this is also a section on the excel table: labeled Reclass and each cell either has a Y for yes and N for no). How would I go about this? Thank you.

Also, the code below keeps repeating itself and makes way more drafts than I need.

 Option Explicit
    'Enumeration is by definition the action of establishing the number of something
    'I Enumerated my Worksheet Columns to give them a meaningful name that is easy to recognize so if the amount is ever moved

    Public Enum EmailColumn
        ecEmailAdresses = 17
        ecSubject = 43
    End Enum
    Public Sub SaveEmails()

    Dim r As Long
    Dim ReCol As Range

    For Each ReCol In Worksheets("Report").Range("AP1:AP1047900").Cells
    If ReCol = "Y" Then

        'The With Statement allows the user to "Perform a series of statements on a specified object without specifying the name of the object multiple times"
        '.Cells(.Row.Count, ecEmailAdresses).End(xlUp).Row actually refers to ThisWorkbook.Worksheets("Data insert").Cells(.Rows.Count, ecEmailAdresses).End(xlUp).Row
        With ThisWorkbook.Worksheets("Report")
            '.Cells(): references a cell or range of cells on Worksheets("Data insert")
            '.Cells(.Rows.Count, ecEmailAdresses): References the last cell in column 43 of the worsheet
            '.End(xlUp): Changes the reference from the last cell to the first used cell above the last cell in column 17
            '.Cells(.Rows.Count, ecEmailAdressess).End(xlUp).Row: returns the Row number of the last cell column 17
            For r = 2 To .Cells(.Rows.Count, ecEmailAdresses).End(xlUp).Row
                getTemplate(MailTo:=.Cells(r, ecEmailAdresses), Subject:=.Cells(r, ecSubject)).Save
            Next
        End With
     End If
     Next ReCol

    End Sub
    Public Function getPOAccrualTemplate(MailTo As String, Optional CC As String, Optional BC As String, Optional Subject As String) As Object
        Const TEMPLATE_PATH As String = "C:\Users\JohnDoe\Documents\Project\ Email Template.oft"

        Dim OutApp As Object
        Dim OutMail As Object
        'CreateObject("Outlook.Application"): Creates an instance of an Outlook Application.
        'Outlook.Application.CreatItemFromTemplate returns a new MailItem Based on a saved email Template
        Set OutMail = CreateObject("Outlook.Application").CreateItemFromTemplate(TEMPLATE_PATH)

        With OutMail
            .To = MailTo
            .CC = CC
            .BCC = BC
            .Subject = Subject
        End With

        'Returns the new MailItem to the caller of the function
        Set getTemplate = OutMail

    End Function

A few problems.

Public Function getPOAccrualTemplate(MailTo As String, Optional CC As String, Optional BC As String, Optional Subject As String) As Object which includes Set getTemplate = OutMail . Should be (notwithstanding other inefficient coding practices):

Public Function getPOAccrualTemplate(MailTo As String, Optional CC As String, Optional BC As String, Optional Subject As String) As Object
    Const TEMPLATE_PATH As String = "C:\Users\JohnDoe\Documents\Project\PO Accrual Push Back Email Template.oft"
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutMail = CreateObject("Outlook.Application").CreateItemFromTemplate(TEMPLATE_PATH)
    With OutMail
        .To = MailTo
        .CC = CC
        .BCC = BC
        .Subject = Subject
    End With
    Set getPOAccrualTemplate= OutMail
End Function

Your loop in SaveEmails is doing exactly what you are asking it to, creating the multiple templates. Each time you have a "Y" you then loop through all the rows and create an e-mail, effectively squaring the number of emails needed. If I understand your logic and your datasheet correctly, removing the loop should solve the repetition problem (Notwithstanding other inefficient coding).

   If ReCol = "Y" Then
        With ThisWorkbook.Worksheets("Report")
                getTemplate(MailTo:=.Cells(Recol.Row, ecEmailAdresses), Subject:=.Cells(Recol.Row, ecSubject)).Save
        End With
     End If

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