【发布时间】:2018-07-06 18:08:55
【问题描述】:
此代码根据我制作的模板和 Excel 表格中的信息(邮件的 MailtTo 和主题行)生成一封电子邮件。
它保存在 Outlook 中的草稿文件夹中,以便稍后发送。
如何在 Outlook 中创建一个名为“重新分类”的新文件夹,该文件夹仍属于这些电子邮件将转到的草稿类别?
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 ReCol As Range 'Relcass Column Range
'For Eeach: picking up the reclass section on the OP Report as a renage
For Each ReCol In Worksheets("Report").Range("AP1:AP1047900")
'If:Running through Reclass column for only Y respones
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(.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 44
'.Cells(.Rows.Count, ecEmailAdressess).End(xlUp).Row: returns the Row number of the last cell column 44
getTemplate(MailTo:=.Cells(ReCol.Row, ecEmailAdresses), Subject:=.Cells(ReCol.Row, ecSubject)).Save
End With
End If
Next
End Sub
Public Function getTemplate(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
【问题讨论】:
-
take a look at this 更好地了解将邮件项目移动到其他文件夹