windows Outlook VBA - MoveTo方法不接受我的Folder变量作为对象

n9vozmp4  于 2023-02-16  发布在  Windows
关注(0)|答案(1)|浏览(211)

所有人,
我写了一些VBA代码,将Outlook会话中当前选定的电子邮件文件夹移动到名为"archive 2023"的文件夹中,没有起作用;我花了很长时间来解决这个问题,但却无法修复代码。最终,我通过研究解决了这个问题,然后从另一个Angular 解决了这个问题。虽然我很高兴,学到了很多东西,但我仍然不知道为什么我原来的代码不起作用。这让我很困扰(如果你原谅我的双关语)。
我将从我的原始代码开始,它不起作用--我已经在注解上花了很多钱。

Sub archive_a_folder()
    
    'firsty create the variable I'll store the current folder in as an object
    Dim current_folder As Outlook.Folder 
    
   'then put the folder, selected in the active instance of Outlook, into the variable
    Set current_folder = Application.ActiveExplorer.CurrentFolder
    
    Debug.Print current_folder.Name 'I put this in to check the above worked - and it did!
    
    'I wrote a little code to find out the EntryID property of the folder called "archive 2023"
    'I then put the EntryID (which is a string) into a variable
    Dim archiveID As String
    archiveID = "xxxx" 'instead of xxxx this is a really long string
    
    'I then create a MAPI namespace so I can use the GetFolderFromID method
    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")
    
    'I then create an Outlook.Folder variable and put the "archive 2023" folder in there by ...
    '... using the GetFolderFromID method using the EntryID

    Dim archive_folder As Outlook.Folder
    Set archive_folder = ns.GetFolderFromID(archiveID)
    
    Debug.Print archive_folder.Name 'Did this to check the above works and it does!

    'So at this point I thought I had two correctly assigned Outlook.Folder object variables ...
    '... One assigned with the folder that needs moving and one being the destination folder
    
    'The documentation states the MoveTo method should be used like this...
    '... Folder.MoveTo(Folder) with the first Folder (an object) being moved to the second.

    current_folder.MoveTo(archive_folder)

    'I get an object expected error.
        
End Sub

逐行运行代码证明,直到current_folder.MoveTo(archive_folder),一切都正常。
调试输出显示变量current_folderarchive_folder被正确地赋值。我甚至打印出变量的类型以确保它们都是Folder类型,并且它们是(它们实际上是FolderMAPI类型,但我认为这是可以的)。
我尝试创建一个新的Folder.Outlook变量,并使用以下语句:
set new_folder = current_folder.MoveTo(archive_folder)

new_folder = current_folder.MoveTo(archive_folder)
但是没有成功(我看到MoveTo方法返回了一个Folder,所以我尝试了这个方法。
最后,经过研究,我重写了是这样的,它的工作。

Sub archive_folder()
    
    'get the current folder and put it in a Folder variable
    Dim current_folder As Outlook.Folder
    Set current_folder = Application.ActiveExplorer.CurrentFolder
    
    'get a namespace variable so I can use some of its methods later
    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")
    
    'create inbox as a Folder variable
    Dim inbox As Outlook.Folder
    
    'using a namespace method assign the actual in-box to the inbox variable
    'olFolderInbox is an inbuilt referene to the default in box folder
    Set inbox = ns.GetDefaultFolder(olFolderInbox)
    
    'create a Foler variable that will be assigned the destination folder
    Dim archive_folder As Outlook.Folder
    
    'this seems oddly cumbersome but works!
    'take parent of the inbox Folder and look for "archive 2003" beneath it
    'assign this to the archive folder variable.
    Set archive_folder = inbox.Parent.Folders("archive 2023")
    
    'The using the MoveTo method move the current_folder to the
    'archive folder
    current_folder.MoveTo archive_folder
           
    'when I check in my Outlook window, its moved!
           
    Exit Sub
       
End Sub

如果我必须猜测问题出在哪里,那就是GetFolderFromID没有返回一个Folder对象,该对象具有MoveTo方法工作所需的所有属性。
我可能认为太'现实世界'错误地想象文件夹实际上被存储在其他文件夹中。系统可能只是看起来像所有文件夹的ParentFolders属性,并为GUI构建一个树。可能GetFolderFromID没有正确返回这些属性值,所以MoveTo不认为它是一个对象,在所有这一错误。如果MoveTo只是弄乱了一些父/文件夹属性,这似乎是可能的。
如果是这样的话,GetFolderFromID函数的意义何在?
或者我是因为试图跳过学习一门语言的基础知识而受到惩罚。
有人帮忙吗?
阿尔杜斯
编辑:
我不敢相信我没有记录我不应该在MoveTo方法中使用括号。@@DmitryStreblechenko在评论中看到了我。
为了让我感觉更好我大大减少了代码的大小...

Sub archive_a_folder()
          
    archiveID = "xxx" `xxx is the EntrhyID of the destination folder  
    Application.ActiveExplorer.CurrentFolder.MoveTo Application.GetNamespace("MAPI").GetFolderFromID(archiveID)
    
End Sub

:-)
sqxo8psd

sqxo8psd1#

我想表明这个问题已经得到了回答,但我想澄清的是,这个问题是在评论中得到回答的,而不是由我来回答的。本质上,我不敢相信我犯了这个错误,我在不应该使用括号的时候使用了括号。
在使用current_folder.MoveTo(archive_folder)的地方,我应该使用current_folder.MoveTo archive_folder
我中了一个老把戏,假设我的语法是正确的,但有一些更深层次的问题。但没有,我只是用了几个括号!
很多年前我曾经写过代码,所以在学习一门新语言时,我有足够的理解,可以冒着跳过一些基础知识的风险,但这种危险就是导致这个问题的原因。
哦,好吧,活到老学到老。

相关问题