对共享文件夹的引用在第一个 运行 之后丢失

Reference to shared folder lost after first run

我想将电子邮件从共享收件箱移动到同一收件箱中的共享子文件夹。

我有两个邮箱,共享的是第二个。

“出站 TTA”= 第二个共享邮箱的名称。

“réception”=“出站 TTA”中收件箱文件夹的名称

"MyFolderEmails" = "réception" 文件夹中子文件夹的名称。

代码有效仅一次

第二次显示

Run-time error '-2147221233 (8004010f)':The attempted operation failed. An object could not be found.

错误出现在Set sharedDestinationFolder = sharedInbox.Folders("MyFolderEmails")

Public Sub test2()
    MoveSelectionToFolder
End Sub


Private Sub MoveSelectionToFolder()
    Dim NS As nameSpace

    Dim sharedInbox As folder
    Dim sharedDestinationFolder As folder

    Dim sharedItems As Selection

    Dim i As Long

    Set NS = Application.GetNamespace("MAPI")
    Set sharedInbox = NS.Folders("Outbound TTA").Folders("réception")
    Set sharedDestinationFolder = sharedInbox.Folders("MyFolderEmails")
        
    Set sharedItems = ActiveExplorer.Selection

    For i = sharedItems.Count To 1 Step -1
        sharedItems(i).Move sharedDestinationFolder
    Next i

    Set NS = Nothing
    Set sharedItems = Nothing
    Set sharedInbox = Nothing
    Set sharedDestinationFolder = Nothing

End Sub

另外,相似的代码也是同归于尽,只能工作一次;之后他们显示错误。

好的,尝试像这样将对文件夹的引用保存为静态变量。 (重启你的outlook然后使用代码)

' Set it as a static variable
Global sharedDestinationFolder As Folder

Public Sub test2()
    MoveSelectionToFolder
End Sub


Private Sub MoveSelectionToFolder()
    Dim NS As Namespace

    Dim sharedInbox As Folder
    'Dim sharedDestinationFolder As Folder

    Dim sharedItems As Selection

    Dim i As Long
       
    If sharedDestinationFolder Is Nothing Then
        Set NS = Application.GetNamespace("MAPI")
        Set sharedInbox = NS.Folders("Outbound TTA").Folders("réception")
        Set sharedDestinationFolder = sharedInbox.Folders("MyFolderEmails")
        
        MsgBox "Setting destination folder"
    End If
        
    Set sharedItems = ActiveExplorer.Selection

    For i = sharedItems.Count To 1 Step -1
        If TypeName(sharedItems(i)) = "MailItem" Then
            sharedItems(i).Move sharedDestinationFolder
        End If
    Next i

    Set NS = Nothing
    Set sharedItems = Nothing
    Set sharedInbox = Nothing
    'Set sharedDestinationFolder = Nothing

End Sub

理论上,您第一次 运行 代码时会看到消息框。当你再次 运行 它时,它不会是 Nothing 因此,它应该有一个对正确文件夹的有效引用(理论上也是如此)