将邮件从本地文件夹复制到公共/共享文件夹

时间:2014-10-07 17:04:34

标签: outlook outlook-vba outlook-2013

当本地文件夹有5封电子邮件时,该Outlook宏应该要求用户将它们移动到公共共享文件夹并删除原来的5封电子邮件。

我一直得到这个"操作失败"错误信息在下面一行。

 **Set NewFolder = myFolder.CopyTo(PblcSharedFolder)**

来自评论:此行旨在复制五封电子邮件的整个文件夹。

Sub MoveEmail()
    Dim app As New Outlook.Application
    Dim nameSpace As Outlook.nameSpace
    Dim currentExplorer As Outlook.Explorer
    Dim currentSelection As Outlook.selection
    Dim currentMailItem As MailItem
    Dim emailCopy As MailItem
    Dim myFolder
    Dim PblcSharedFolder As MAPIFolder
    Dim NewFolder As MAPIFolder
    Dim i As Integer
    Dim folderName
    Dim mailboxNameString
    Dim result

    folderName = "Local Folder"
    mailboxNameString = "My Inbox Name"
    Set nameSpace = app.GetNamespace("MAPI")
    Set currentExplorer = app.ActiveExplorer
    Set currentSelection = currentExplorer.selection
    Set myFolder = nameSpace.Folders(mailboxNameString).Folders("Inbox").Folders(folderName)

    If (myFolder.Items.Count = 5) Then
        result = MsgBox("Would you like to move the content of your Local folder to the Public-shared Folder?", vbYesNo)

        If result = vbYes Then

            Set PblcSharedFolder = nameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders)
            Set PblcSharedFolder = PblcSharedFolder.Folders("Subfolder")
            Set PblcSharedFolder = PblcSharedFolder.Folders("Public Shared Folder")

            Set NewFolder = myFolder.CopyTo(PblcSharedFolder)

            NewFolder.Name = "_" & nameSpace.CurrentUser.Name & " " & Now

            For i = myFolder.Items.Count To 1 Step -1
                myFolder.Items(i).Delete
            Next i

        Else

            Stop

        End If
    End If

    For i = 1 To currentSelection.Count
       Set currentMailItem = currentSelection.Item(i)
       Debug.Print "[" & Date & " " & Time & "] moving #" & m & _
                   ": xfolder = " & folderName & _
                   "; subject = " & currentMailItem.Subject & "..."
       currentMailItem.To = nameSpace.CurrentUser.Name
       currentMailItem.Move myFolder
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

未测试!

If result = vbYes Then

        Set PblcSharedFolder = nameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders)
        Set PblcSharedFolder = PblcSharedFolder.Folders("Subfolder")
        Set PblcSharedFolder = PblcSharedFolder.Folders("Public Shared Folder")
        Dim myCopiedItem As Outlook.MailItem 


        For i = myFolder.Items.Count To 1 Step -1
            Set myCopiedItem = myFolder.Items(i).Copy 
            myCopiedItem.Move PblcSharedFolder 
            myFolder.Items(i).Delete
        Next i
    Else
        Stop
    End If

我说实话并不是很清楚这条线的用途:

    NewFolder.Name = "_" & nameSpace.CurrentUser.Name & " " & Now