我的Outlook宏一次工作,然后停止。解析时,宏会抛出错误“编译错误:变量未定义”。似乎宏不识别该文件夹存在。我把代码削减到最低限度,这是一个可重复的问题。宏将识别标准文件夹,如JUNK和DRAFTS,但不识别PROCESSED_FOLDERS。我尝试重命名Processed_Folders以及创建一个具有不同名称的新文件夹。没有快乐。
文件夹结构是:
reports@xxx.com
Inbox
Drafts
Sent
Trash
Junk
Processed_Reports
Outbox
Sync Issues1 (This computer only)
SearchFolders
CODE:
Sub testfforfolder()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
On Error GoTo xyz
Set olFolder = olFolder.Folders("Processed_Reports")
MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
Exit Sub
xyz:
MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
Exit Sub
End Sub
答案 0 :(得分:0)
感谢Tony Dallimore的评论,我能够解决这个问题。 https://stackoverflow.com/a/12146315/973283的链接让我解决了这个问题。使用Outlook的更新版本,正在引用默认电子邮件帐户,而不是所选项目的帐户。 Processed_Reports文件夹仅存在于其他帐户文件夹中。正如Tony建议的那样,解决方案是将目标文件夹设置为目标的完整路径。我确实需要一个级别,如下面的工作解决方案所示。
Sub testfforfolder()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
On Error GoTo xyz
'OLD INCORRECT
'Set olFolder = olFolder.Folders("Processed_Reports")
'WORKING CORRECTION
Set TgtFolder= _
Session.Folders("reports@xxx.com"). _
Folders("Inbox").Folders("Processed_Reports")
MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
Exit Sub
xyz:
MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
Exit Sub
End Sub
答案 1 :(得分:0)
这是一种在不知道会话名称的情况下获取会话文件夹的方法
Sub topFolder()
Dim topFolder As Folder
Set topFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent
Dim i As Integer
For i = 1 To topFolder.Folders.Count
Debug.Print topFolder.Folders(i).Name
Next i
For i = 1 To topFolder.Folders("inbox").Folders.Count
Debug.Print topFolder.Folders("inbox").Folders(i).Name
Next i
End Sub