VBA打开用户邮箱,搜索文件夹,选择最新的电子邮件,保存附件,打开该附件

时间:2014-08-24 08:05:34

标签: vba outlook outlook-vba

好的,主题说的大部分内容,希望我的代码片段能够详细说明。

Sub dSearchUserFolder()
'set rig email and filename root strings
addressee = "RigBig1.Drill@money.com"
subjectRoot = "Daily Drill"
wellId = "RM01-1-1"

Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim myFolder As Outlook.MAPIFolder

'open users inbox folder and search and select most recent

Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("coyrigs")
myRecipient.Resolve

If myRecipient.Resolved Then
Set myFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderInbox)

'filter by addresse and subject (subjectRoot)
Items = inboxFolder.Items.Restrict(xxxxx)
'select most recent


'save attachment with filename matching "Daily Drilling*.pdf"

FileName = %HOME% & "/Dropbox/Drilling/" & wellId Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                Atmt.SaveAsFile FileName
'open attachment


End Sub

我打算通过大约一天或两天的谷歌搜索来解决这种或那种方式。我认为这对其他人很感兴趣,因为我可以通过搜索像我这样的新手来解决一系列小问题,因此我在这里发帖。但是,如果那里的任何人想要帮助,我们将非常感激。

1 个答案:

答案 0 :(得分:0)

你几乎就在那里 - 调用Items.Sort按[ReceivedTime]排序,然后遍历Items集合。对于每个MailItem对象,循环遍历附件并调用Attachment.SaveAsFile。