excel - 使用 Excel VBA 将 Outlook 电子邮件从收件箱移动到基于主题行的存档子文件夹 (Outlook 365 - Microsoft Exchange)
问题描述
我使用的 Outlook 版本是通过雇主,他们使用 Outlook 365 - Microsoft Exchange(owa)。
我编写了一个脚本,在我的 Outlook 收件箱中查找包含“PHI Attrition Dashboard Terminations”的任何主题行的电子邮件。找到后,它会检查以确保这是一封尚未审核的新电子邮件并包含附件。它将电子邮件中的附件保存到共享驱动器上的文件夹并重命名文件以包含适用日期。然后,根据用户的选择,调用另一个宏来完成额外的更新。所有这部分工作完美,我遇到困难的部分是一旦调用的宏完成并返回到这个宏,我希望能够将使用过的电子邮件移动到另一个文件夹中,该文件夹保存在我的存档项目下在 Outlook 中。我想不出引用存档子文件夹的方法。我在下面包含了我的代码,以及我的 Outlook 文件层次结构的屏幕截图。我正在尝试将电子邮件从我的收件箱移动到存档下的“文件更新”文件夹。
对于 Set SubFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("File Updates") 行,我使用了这种格式,我尝试过 SubFolder = Inbox.Folders("File Updates") 并且似乎没有任何效果。我不断收到错误:
当前代码:
Sub CheckEmail_HRT()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Declare Outlook Objects
Dim olApp As New Outlook.Application
Dim olNamespace As Outlook.Namespace 'Same as olNs
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
'Declare other variables
Dim filteredItems As Outlook.Items 'Same as Items
Dim itm As Object 'Same as Item
Dim strFilter As String
'Outlook Variables for email
Dim sSubj As String, dtRecvd As String 'sSubj same as strSubjec
Dim oldSubj As String, olddtRecvd As String
Dim olFileName As String, olFileType As String
Dim strFolder As String
Sheets("Job Mapping").Visible = True
Sheets("CC Mapping").Visible = True
Sheets("Site Mapping").Visible = True
Sheets("Historical Blue Recruit Data").Visible = True
Sheets("Historical HRT Data").Visible = True
Sheets("Combined Attrition Data").Visible = True
Sheets.Add Before:=Sheets(1)
'Designate ECP Facilities Model file as FNAME
myPath = ThisWorkbook.Path
MainWorkbook = ThisWorkbook.Name
Range("A1").Select
ActiveCell.FormulaR1C1 = myPath
'designate file path for Attrition Files
FacModPath = Cells(1, 1).Value
Sheets(1).Delete
'Get Outlook Instance
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set Inbox = olNamespace.GetDefaultFolder(olFolderInbox)
Set SubFolder = olNamespace.***Unsure of Code here****.Folders("File Updates")
strFilter = "@SQL=urn:schemas:httpmail:subject LIKE '%PHI Attrition Dashboard Terminations%'"
Set filteredItems = Inbox.Items.Restrict(strFilter)
'Chec if there are any matching emails
If filteredItems.Count = 0 Then
MsgBox "No emails found."
GoTo ExitFor
Else
For Each itm In filteredItems
If itm.Attachments.Count <> 0 Then
dtRecvd = itm.ReceivedTime
dtRecvd = Format(dtRecvd, "mm/dd/yyyy")
sSubj = itm.Subject
oldSubj = Sheets("CC Mapping").Range("N2").Value
olddtRecvd = Sheets("CC Mapping").Range("N3").Value
olddtRecvd = Format(olddtRecvd, "mm/dd/yyyy")
If sSubj = oldSubj And dtRecvd <= olddtRecvd Then
MsgBox "No new HRT data files to load."
GoTo ExitFor
Else
Workbooks(MainWorkbook).Activate
If Sheets("CC Mapping").Visible = False Then
Sheets("CC Mapping").Visible = True
End If
Sheets("CC Mapping").Select
Range("N2").Select
ActiveCell.FormulaR1C1 = sSubj
Range("N3").Select
ActiveCell.FormulaR1C1 = dtRecvd
For j = 1 To itm.Attachments.Count
olFileName = itm.Attachments.Item(1).DisplayName
If Right(LCase(olFileName), 4) = ".xls" Then
'Query if user wishes to contunue to load data
Answer = MsgBox("New HRT Attrition Dasboard Terminations attachment found, dated " & dtRecvd & "." & vbNewLine & "Would you like to load the new data?", vbQuestion + vbYesNo, "Confirm Next Step")
If Answer = vbYes Then
olFileName = "HRT_ATTRITION_DASHBOARD_TERMS-" & Format(dtRecvd, "MM.DD.YY") & ".xls"
itm.Attachments.Item(1).SaveAsFile FacModPath & "\" & olFileName
Call HRT_Update
Else
GoTo ExitFor
End If
Else
MsgBox "No attachment found."
GoTo ExitFor
End If
Next j
End If
End If
'Mark email as read
itm.UnRead = False
'Move email to SubFolder
itm.Move SubFolder
Next
End If
ExitFor:
Sheets("Job Mapping").Visible = False
Sheets("CC Mapping").Visible = False
Sheets("Site Mapping").Visible = False
Sheets("Historical Blue Recruit Data").Visible = True
Sheets("Historical HRT Data").Visible = True
Sheets("Combined Attrition Data").Visible = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
解决方案
您几乎就在那里 - 从收件箱,向上一层到其父文件夹,然后到存档文件夹,然后到子文件夹
set Inbox = olNamespace.GetDefaultFolder(olFolderInbox)
set InboxParent = Inbox.Parent
set Archive = InboxParent.Folders("Archive")
set DestFolder = Archive.Folders("File Updates")
请注意,存档文件夹是默认文件夹之一,但 Outlook 对象模型不会将其公开。由于实际名称可以本地化,因此您可能会在本地化环境中遇到问题。例如,Redemption允许您使用RDOSession打开存档文件夹。GetDefaultFolder(olFolderArchive)
.
推荐阅读
- streaming - 只有一个 CPU 内核处理网络中断
- jupyter-notebook - Jupyterlab 扩展离线安装
- c++ - 如何解决自定义类对象的 unordered_set hasher 函数中的这个错误?
- oauth - Dart:客户端的 Websocket Bearer 身份验证
- oracle - 在 INSERT 语句中使用绑定变量
- sql - 触发器中的外部表引用
- javascript - Typescript 将 es6 .js 依赖项转换为 es5
- node.js - 调试电子/节点应用程序时如何显示网络面板
- r - R:ggarange 增加多图的图标题和每个图的标题之间的距离
- javascript - Ajax附加下拉提交空值