简体   繁体   English

If statement/Outlook/VBA : 起草电子邮件

[英]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 . MailTo 和 Subject 是从 Excel 数据表中提取的: ecEmailAdresses = 17ecSubject = 43 The line it which I need help on is the ** If Statement ** .我需要帮助的那一行是** 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).我希望宏只在此人提交重新分类时起草一封电子邮件(这也是 excel 表上的一个部分:标记为重新分类,每个单元格都有一个 Y 表示是,N 表示否)。 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 . Public Function getPOAccrualTemplate(MailTo As String, Optional CC As String, Optional BC As String, Optional Subject As String) As Object其中包括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.您在SaveEmails循环完全按照您的要求执行,创建多个模板。 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.每次你有一个“Y”,你就遍历所有的行并创建一个电子邮件,有效地对所需的电子邮件数量进行平方。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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