0

I tried to write VBA code to move the currently selected email folder in an Outlook session to a folder called "archive 2023".

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

Running the code line by line proved everything was working up to current_folder.MoveTo(archive_folder).

The debugging print outs show that the variables current_folder and archive_folder are correctly assigned. I printed out the variables' types to ensure they were both of the Folder type. (They were type FolderMAPI but I think that's OK.)

I tried creating a new Folder.Outlook variable and having the below statement:

set new_folder = current_folder.MoveTo(archive_folder)

or

new_folder = current_folder.MoveTo(archive_folder)

I saw that the MoveTo method returned a Folder so that's why I tried that.


I re-wrote it and it worked.

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

I should not have used parentheses for the MoveTo method. DmitryStreblechenko saw me right in the comments.

I massively reduced the size of the code.

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
Community
  • 1
  • 1
Aldus_III
  • 1
  • 1

1 Answers1

0

I want to show this question as answered but want to make clear it was answered in the comments and not by me. In essence, and I can't believe I made this mistake, I used parenthesis when I should not have.

Where I used current_folder.MoveTo(archive_folder) I should have used current_folder.MoveTo archive_folder

I fell for the old trick of assuming my syntax was correct but there was some deeper problem. But no, I just used a couple of brackets!

I used to code many years ago so have enough of an understanding to risk skipping some of the basics when learning a new language but the danger of that is what led to this question.

Oh well, you live and learn.

Aldus_III
  • 1
  • 1