下面是我编写的从excel表中提取信息的代码
到Outlook模板。我希望这段代码能做另外两件事,但我很难弄清楚。
使用一个If语句(我认为),以便只有在该行中有Y的用户(这是我的excel表的一部分,我将从中提取该部分用于电子邮件的MailTo部分)(表中的“重分类”列中包含Y或N)将起草一封电子邮件,以供日后发送。
Public Enum EmailColumns
ecEmailAdresses = 44
ecSubject = 43
End Enum
Public Sub SaveEmails()
Dim r As Long
'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 44
'.Cells(.Rows.Count, ecEmailAdressess).End(xlUp).Row: returns the Row number of the last cell column 44
For r = 2 To .Cells(.Rows.Count, ecEmailAdresses).End(xlUp).Row
getPOAccrualTemplate(MailTo:=.Cells(r, ecEmailAdresses), Subject:=.Cells(r, ecSubject)).Save
Next
End With
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\JoeDoe\Documents\Project\Email Template.oft"
Dim OutApp As Object, 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
答案 0 :(得分:0)
使用Application.CreateItemFromTemplate
,而不使用MAPIFolder.Items.Add
,其中MAPIFolder
是从OOM检索的文件夹。假设该文件夹与“草稿”文件夹处于同一级别,请尝试以下操作:
set app = CreateObject("Outlook.Application")
set ns = app.GetNamespace("MAPI")
ns.Logon
set drafts = ns.GetDefaultFolder(olFolderDrafts)
on error resume next 'the line below can raise an exception if the folder is not found
set myFolder = drafts.Parent.Folders("Reclass")
if myFolder Is Nothing Then
set myFolder = drafts.Parent.Folders.Add("Reclass")
end If
set OutMail = myFolder.Items.Add