【发布时间】:2020-06-30 20:09:35
【问题描述】:
我目前正在编写 Outlook 中的 VBA 代码以作为宏运行,该宏将帮助查找从主邮箱、辅助邮箱甚至存档邮箱 (PST) 错误移动的文件夹。
当前运行时的代码将离线切换 Outlook,询问您的文件夹名称(可以部分作为通配符搜索),返回找到的名称的第一个实例并带您到文件夹,最后将 Outlook 恢复到在线模式。
我正试图找出让它迭代所有搜索实例的过程(例如,主邮箱和档案中名为“April”的多个文件夹)。我知道它可能需要一个 do until 循环并指定一个计数器,但不确定如何实现。
这是当前的工作代码:
Sub ToggleWorkOfflineMode()
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
If Not OutApp.Session.Offline = True Then
If MsgBox("Do you want to enable Work Offline Status?", vbQuestion Or vbYesNo) = vbYes Then
OutApp.GetNamespace("MAPI").Folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613).Execute
Else
MsgBox "Status Not Changed.", vbInformation
End If
Else
If MsgBox("Do you Want to disable Work Offline Status?", vbQuestion Or vbYesNo) = vbNo Then
MsgBox "Working offline", vbInformation
Else
OutApp.GetNamespace("MAPI").Folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613).Execute
End If
End If
End Sub
Sub FindFolderByName()
Dim Name As String
Dim FoundFolder As Folder
Name = InputBox("Find Name:", "Search Folder")
If Len(Trim$(Name)) = 0 Then Exit Sub
Set FoundFouder = FindInFolders(Application.Session.Folders, Name)
If Not FoundFouder Is Nothing Then
If MsgBox("Activate Folder: " & vbCrLf & FoundFouder.FolderPath, vbQuestion Or vbYesNo) = vbYes Then
Set Application.ActiveExplorer.CurrentFolder = FoundFouder
End If
Else
MsgBox "Not Found", vbInformation
End If
End Sub
Function FindInFolders(TheFolders As Outlook.Folders, Name As String)
Dim SubFolder As Outlook.MAPIFolder
On Error Resume Next
Set FindInFolders = Nothing
For Each SubFolder In TheFolders
If LCase(SubFolder.Name) Like LCase(Name) Then
Set FindInFolders = SubFolder
Exit For
Else
Set FindInFolders = FindInFolders(SubFolder.Folders, Name)
If Not FindInFolders Is Nothing Then Exit For
End If
Next
End Function
【问题讨论】:
-
取出
FindInFolders中的Exit For语句,并将结果放入某种收集容器中。 -
要搜索“主邮箱中名称为“April”的多个文件夹”,然后单独搜索“存档中名称为“April”的多个文件夹。stackoverflow.com/questions/27189429/…
-
@niton 使用链接中提供的代码,在
If Not MyFolder Is Nothing Then Exit For的 Loopfolders Sub 内部时,宏因“需要对象”而失败 -
@Comintern which Exit For 语句有两个