簡體   English   中英

If statement/Outlook/VBA : 起草電子郵件

[英]If statement/Outlook/VBA : Drafting Emails

此代碼的目的是為提交重新分類的用戶制作電子郵件草稿。 MailTo 和 Subject 是從 Excel 數據表中提取的: ecEmailAdresses = 17ecSubject = 43 我需要幫助的那一行是** If Statement ** 我希望宏只在此人提交重新分類時起草一封電子郵件(這也是 excel 表上的一個部分:標記為重新分類,每個單元格都有一個 Y 表示是,N 表示否)。 我該怎么辦? 謝謝你。

此外,下面的代碼不斷重復,並且草稿比我需要的多。

 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

幾個問題。

Public Function getPOAccrualTemplate(MailTo As String, Optional CC As String, Optional BC As String, Optional Subject As String) As Object其中包括Set getTemplate = OutMail 應該是(盡管其他低效的編碼實踐):

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

您在SaveEmails循環完全按照您的要求執行,創建多個模板。 每次你有一個“Y”,你就遍歷所有的行並創建一個電子郵件,有效地對所需的電子郵件數量進行平方。 如果我正確理解您的邏輯和數據表,刪除循環應該可以解決重復問題(盡管其他低效編碼)。

   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