5

我编辑了一个我在网上找到的脚本,将电子邮件移动到各种文件夹。

我想更进一步将电子邮件移动到单独的 PST 文件中的文件夹中。

这将在 Outlook 2007 中运行。

该宏源于这个名为“更新”的宏,是更简洁的版本:http:
//jmerrell.com/2011/05/21/outlook-macros-move-email

我几乎可以肯定此链接包含线索,但我没有正确应用它的经验:
http ://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/

我当前的宏允许将电子邮件移动到 PST 主“收件箱”文件夹中的 3 个不同文件夹位置。

'Outlook VB Macro to move selected mail item(s) to a target folder
Sub MoveToFolder(targetFolder)
On Error Resume Next

Dim ns As Outlook.NameSpace
Dim MoveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem

Set ns = Application.GetNamespace("MAPI")

'define path to the target folder; the following assumes the target folder
'is a sub-folder of the main Mailbox folder

'This is the original'
'Set MoveToFolder = ns.Folders("Mailbox").Folders(targetFolder)'
Set MoveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders(targetFolder)


If Application.ActiveExplorer.Selection.Count = 0 Then
    MsgBox ("No item selected")
    Exit Sub
End If

If MoveToFolder Is Nothing Then
    MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If

For Each objItem In Application.ActiveExplorer.Selection
    If MoveToFolder.DefaultItemType = olMailItem Then
        If objItem.Class = olMail Then
            objItem.Move MoveToFolder
        End If
    End If
Next

Set objItem = Nothing
Set MoveToFolder = Nothing
Set ns = Nothing

End Sub

Sub MoveToActive()
MoveToFolder ("Active")
End Sub

Sub MoveToAction()
MoveToFolder ("Action")
End Sub

Sub MoveToOnHold()
MoveToFolder ("OnHold")
End Sub

如何配置第四个选项以将电子邮件移动到不同 PST 中的文件夹?

例如,我想添加一个名为“存档”的额外按钮,当单击此特定按钮时,它会将电子邮件移动到单独的 PST 收件箱中的存档文件夹中。

Sub MoveToArchive()
MoveToFolder ("Archive")
End Sub
4

3 回答 3

1

这是一个老问题,但这是一个对我有用的解决方案,从几个来源修改代码。您可以根据您的要求对其进行修改。

这允许用户选择任何文件夹,无论是在默认位置下,还是在另一个存档或 PST 文件中。如果用户在文件夹选择器中选择取消,则电子邮件将保存到默认的已发送邮件文件夹。

Private Sub Application_ItemSend(ByVal Item As Object, _
                                 Cancel As Boolean)
    Dim objNS As NameSpace
    Dim objFolder As MAPIFolder
    On Error Resume Next
    Set objNS = Application.Session
    If Item.Class = olMail Then
        Set objFolder = objNS.PickFolder

        'save to a folder under the default structure, main PST/archive
        If Not objFolder Is Nothing And IsInDefaultStore(objFolder) And objFolder.DefaultItemType = olMailItem Then
            Set Item.SaveSentMessageFolder = objFolder

        'save to a non-default, different PST/archive
        ElseIf Not IsInDefaultStore(objFolder) Then
            Set objFolder = GetFolderFromPath(objFolder.FolderPath)
            Set Item.SaveSentMessageFolder = objFolder

        'neither, just save to default sent items folder
        Else
            Set objFolder = objNS.GetDefaultFolder(olFolderSentMail)
            Set Item.SaveSentMessageFolder = objFolder
        End If
    End If
    Set objFolder = Nothing
    Set objNS = Nothing
End Sub

Public Function IsInDefaultStore(objOL As Object) As Boolean
    Dim objApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objInbox As Outlook.MAPIFolder
    Dim blnBadObject As Boolean
    On Error Resume Next
    Set objApp = objOL.Application
    If Err = 0 Then
        Set objNS = objApp.Session
        Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
        Select Case objOL.Class
            Case olFolder
                If objOL.StoreID = objInbox.StoreID Then
                    IsInDefaultStore = True
                Else
                    IsInDefaultStore = False
                End If
            Case olAppointment, olContact, olDistributionList, _
                 olJournal, olMail, olNote, olPost, olTask
                If objOL.Parent.StoreID = objInbox.StoreID Then
                    IsInDefaultStore = True
                Else
                    IsInDefaultStore = False
                End If
            Case Else
                blnBadObject = True
        End Select
    Else
        blnBadObject = True
    End If
    If blnBadObject Then
        'if cancel is selected then just leave in sent items, so do nothing.
'        MsgBox "This function isn't designed to work " & _
'               "with " & TypeName(objOL) & _
'                " objects and will return False.", _
'                , "IsInDefaultStore"
        IsInDefaultStore = False
    End If
    Set objApp = Nothing
    Set objNS = Nothing
    Set objInbox = Nothing
End Function

'modified from https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
Function GetFolderFromPath(ByVal FolderPath As String) As Outlook.Folder
    Dim oFolder As Outlook.Folder
    Dim FoldersArray As Variant
    Dim i As Integer

    On Error GoTo GetFolderPath_Error
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
    If Not oFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = oFolder.Folders
            Set oFolder = SubFolders.Item(FoldersArray(i))
            If oFolder Is Nothing Then
                Set GetFolderFromPath = Nothing
            End If
        Next
    End If
    'Return the oFolder
    Set GetFolderFromPath = oFolder
    Exit Function

GetFolderPath_Error:
    Set GetFolderPath = Nothing
    Exit Function
End Function
于 2019-07-18T16:45:00.670 回答
0

试试上面代码中注释掉的这种格式。

Sub MoveToFolderInPST(targetFolder)
    '...
    Set MoveToFolderInPST = ns.Folders("name of pst").Folders("Inbox").Folders(targetFolder)
于 2013-11-01T09:52:29.753 回答
0

也许这会满足你的需要。函数SeekFolder()将返回文件夹或没有找到它的情况下不返回。

Public Function SeekFolder(ByVal SearchName As String) As Folder
    Dim FoundFolder As Folder
    For Each RF In Session.Folders
        Set FoundFolder = RecursiveSearch(RF, SearchName)
        If Not FoundFolder Is Nothing Then
            If FoundFolder = SearchName Then
                Set SeekFolder = FoundFolder
                Exit Function
            End If
        End If
    Next RF
End Function

Private Function RecursiveSearch(ByVal RF As Folder, ByVal SearchName As String) As Folder
    If RF.Folders.Count <= 0 Then Exit Function
    For Each f In RF.Folders
        If LCase(f) = LCase(SearchName) Then
            Set RecursiveSearch = f
            Exit Function
        End If
        If f.Folders.Count > 0 Then RecursiveSearch f, SearchName
    Next f
End Function
于 2020-12-24T10:37:45.907 回答