【问题标题】:One button file emails - Outlook 365一键式文件电子邮件 - Outlook 365
【发布时间】:2018-10-22 10:28:43
【问题描述】:

我正在尝试制作一个一键式文件宏,用于查看目录并将电子邮件归档到相应的文件夹中。 我遇到的问题是我必须有针对每个类别的代码,因为文件夹有不同的路径。有没有办法不必将完整路径放入代码中?

见下例

Sub Move_Email()

Dim itm As MailItem
Dim CATNAME As String
Set itm = ActiveExplorer.Selection(1
If itm.Categories = "Customer1" Then
    itm.Move Session.GetDefaultFolder(olFolderInbox).Folders("01 - My Accounts").Folders("Customer1")
Else
    If itm.Categories = "Supplier1" Then
        itm.Move Session.GetDefaultFolder(olFolderInbox).Folders("01 - My Suppliers").Folders("Supplier1")
    Else
    Exit Sub
    End If
 Exit Sub
 End If
 End Sub

我希望它更像

Sub Move_Email2()

Dim itm As MailItem
Dim CATNAME As String
Set itm = ActiveExplorer.Selection(1)
CATNAME = itm.Categories

If itm.Categories = CATNAME Then
    itm.Move Session.GetDefaultFolder(olFolderInbox).Folders(CATNAME)
End If
End Sub

这可能吗?

尝试 1:

Sub Move_Email2()

Dim itm As MailItem
Dim Name As String
Dim FoundFolder As Folder
Set itm = ActiveExplorer.Selection(1)

Name = itm.Categories

If Len(Trim$(Name)) = 0 Then Exit Sub

Set FoundFolder = FindInFolders(Application.Session.Folders, Name)

If Not FoundFolder Is Nothing Then
    itm.Move Session.GetDefaultFolder(olFolderInbox).Folders(FoundFolder.FolderPath)
End If

End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您可以使用 FolderName 来查找文件夹,例如:

    Sub Move_Email2()
    
    Dim itm As MailItem
    Dim Name As String
    Dim FoundFolderPath As String
    Dim strFolderPath As Folder
    Set itm = ActiveExplorer.Selection(1)
    
    If Len(Trim$(Name)) = 0 Then Exit Sub
    
    For Each Name In itm.Categories
        Set FoundFolder = FindInFolders(Application.Session.Folders, Name)
        If Not FoundFolder Is Nothing Then
            itm.Move GetFolder(FoundFolder.FolderPath)
        End If
    Next
    
    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
    Function GetFolder(ByVal FolderPath As String) As Outlook.Folder
     Dim TestFolder As Outlook.Folder
     Dim FoldersArray As Variant
     Dim i As Integer
    
     On Error GoTo GetFolder_Error
     If Left(FolderPath, 2) = "\\" Then
     FolderPath = Right(FolderPath, Len(FolderPath) - 2)
     End If
     'Convert folderpath to array
     FoldersArray = Split(FolderPath, "\")
     Set TestFolder = Application.Session.Folders.item(FoldersArray(0))
     If Not TestFolder Is Nothing Then
     For i = 1 To UBound(FoldersArray, 1)
     Dim SubFolders As Outlook.Folders
     Set SubFolders = TestFolder.Folders
     Set TestFolder = SubFolders.item(FoldersArray(i))
     If TestFolder Is Nothing Then
     Set GetFolder = Nothing
     End If
     Next
     End If
     'Return the TestFolder
     Set GetFolder = TestFolder
     Exit Function
    
    GetFolder_Error:
     Set GetFolder = Nothing
     Exit Function
    End Function
    

    请参考此链接:

    How To Find Folder By Name In Outlook?

    Obtain a Folder Object from a Folder Path

    【讨论】:

    • 我不完全确定,如何适应。上面的代码“尝试 1”工作并找到文件夹,然后我如何将电子邮件移动到它?而且这似乎比直接路径慢得多,尽管我想没有办法解决这个问题?(2-4 秒而不是即时)
    • 我更新了我的代码,你可以试试这个代码。@Kevin Sharpes
    • 不幸的是,该代码给了我以下错误编译错误:For Each 只能遍历集合对象或数组
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多