gpt4 book ai didi

vba - 首次运行后对共享文件夹的引用丢失

转载 作者:行者123 更新时间:2023-12-03 08:39:47 24 4
gpt4 key购买 nike

我想将电子邮件从共享收件箱移到同一收件箱中的共享子文件夹。
我有两个邮箱,共享的是第二个。
“出站TTA” =第二个共享邮箱的名称。
“接收” =“出站TTA”中收件箱文件夹的名称
“MyFolderEmails” =“接收”文件夹中子文件夹的名称。
该代码只能在一次运行!
第二次显示

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,因此,它应该具有对正确文件夹的有效引用(理论上也是)

关于vba - 首次运行后对共享文件夹的引用丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62822143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com